Skip to content

Commit

Permalink
adds ImVec4
Browse files Browse the repository at this point in the history
  • Loading branch information
ericoporto committed Jun 6, 2020
1 parent e6db270 commit 1feeea6
Show file tree
Hide file tree
Showing 6 changed files with 313 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Expand Up @@ -50,6 +50,8 @@ add_library(agsimgui SHARED
agsimgui/agsimgui.h
agsimgui/AgsImVec2.cpp
agsimgui/AgsImVec2.h
agsimgui/AgsImVec4.cpp
agsimgui/AgsImVec4.h
agsimgui/SerialHelper.cpp
agsimgui/SerialHelper.h
agsimgui/agsimgui.cpp
Expand Down
10 changes: 5 additions & 5 deletions agsimgui/AgsImVec2.cpp
Expand Up @@ -57,12 +57,12 @@ int AgsImVec2Interface::Dispose(const char* address, bool force)

int AgsImVec2Interface::Serialize(const char* address, char* buffer, int bufsize)
{
AgsImVec2* imvec2 = (AgsImVec2*)address;
AgsImVec2* agsImVec2 = (AgsImVec2*)address;
char* ptr = buffer;
char* end = buffer + bufsize;

ptr = FloatToChar(imvec2->x, ptr, end);
ptr = FloatToChar(imvec2->y, ptr, end);
ptr = FloatToChar(agsImVec2->x, ptr, end);
ptr = FloatToChar(agsImVec2->y, ptr, end);

return (ptr - buffer);
}
Expand All @@ -79,9 +79,9 @@ void AgsImVec2Reader::Unserialize(int key, const char* serializedData, int dataS
ptr = CharToFloat( val_x, ptr);
ptr = CharToFloat( val_y, ptr);

AgsImVec2* imvec2 = new AgsImVec2(val_x, val_y, key);
AgsImVec2* agsImVec2 = new AgsImVec2(val_x, val_y, key);

engine->RegisterUnserializedObject(key, imvec2, &AgsImVec2_Interface);
engine->RegisterUnserializedObject(key, agsImVec2, &AgsImVec2_Interface);
}

//..............................................................................
Expand Down
8 changes: 4 additions & 4 deletions agsimgui/AgsImVec2.h
Expand Up @@ -16,13 +16,13 @@
struct AgsImVec2 : ImVec2 {
public :
int id;
AgsImVec2(float x, float y)
: ImVec2(x,y){
AgsImVec2(float _x, float _y)
: ImVec2(_x,_y){
id = -1;
}

AgsImVec2(float x, float y, int _id)
: ImVec2(x,y){
AgsImVec2(float _x, float _y, int _id)
: ImVec2(_x,_y){
id = _id;
}
float Length() ;
Expand Down
94 changes: 94 additions & 0 deletions agsimgui/AgsImVec4.cpp
@@ -0,0 +1,94 @@
/*
* Copyright (C) 4040 Érico Vieira Porto
*
* This program is free software. You can use and redistribute it
* under the terms and conditions of the MIT License (see LICENCE).
*/

#include <cmath>
#include "AgsImVec4.h"

float AgsImVec4::Length() {
return sqrt(x * x + y * y + z * z + w * w);
}

AgsImVec4* AgsImVec4::Scale(float scale) {
return new AgsImVec4(x * scale, y * scale, z*scale, w*scale);
}

float AgsImVec4::SquaredLength(){
return (x * x + y * y + z * z + w * w);
}

AgsImVec4* AgsImVec4::Add(AgsImVec4* agsImVec4) {
if(agsImVec4 == nullptr) return new AgsImVec4(x , y , z, w);

return new AgsImVec4(x + agsImVec4->x, y + agsImVec4->y, z + agsImVec4->z, w + agsImVec4->w);
}

AgsImVec4* AgsImVec4::Sub(AgsImVec4* agsImVec4) {
if(agsImVec4 == nullptr) return new AgsImVec4(x , y, z, w);

return new AgsImVec4(x - agsImVec4->x, y - agsImVec4->y, z - agsImVec4->z, w - agsImVec4->w);
}



//------------------------------------------------------------------------------

extern IAGSEngine* engine;

AgsImVec4Interface AgsImVec4_Interface;
AgsImVec4Reader AgsImVec4_Reader;

const char* AgsImVec4Interface::name = "ImVec4";

//------------------------------------------------------------------------------
#include "SerialHelper.h"
using namespace SerialHelper;

int AgsImVec4Interface::Dispose(const char* address, bool force)
{
delete ((AgsImVec4*)address);
return (1);
}

//------------------------------------------------------------------------------

int AgsImVec4Interface::Serialize(const char* address, char* buffer, int bufsize)
{
AgsImVec4* agsImVec4 = (AgsImVec4*)address;
char* ptr = buffer;
char* end = buffer + bufsize;

ptr = FloatToChar(agsImVec4->x, ptr, end);
ptr = FloatToChar(agsImVec4->y, ptr, end);
ptr = FloatToChar(agsImVec4->z, ptr, end);
ptr = FloatToChar(agsImVec4->w, ptr, end);

return (ptr - buffer);
}

//------------------------------------------------------------------------------

void AgsImVec4Reader::Unserialize(int key, const char* serializedData, int dataSize)
{
char* ptr = (char*) serializedData;

float val_x;
float val_y;
float val_z;
float val_w;

ptr = CharToFloat( val_x, ptr);
ptr = CharToFloat( val_y, ptr);
ptr = CharToFloat( val_z, ptr);
ptr = CharToFloat( val_w, ptr);

AgsImVec4* agsImVec4 = new AgsImVec4(val_x, val_y, val_z, val_w, key);

engine->RegisterUnserializedObject(key, agsImVec4, &AgsImVec4_Interface);
}

//..............................................................................

74 changes: 74 additions & 0 deletions agsimgui/AgsImVec4.h
@@ -0,0 +1,74 @@
/*
* Copyright (C) 4040 Érico Vieira Porto
*
* This program is free software. You can use and redistribute it
* under the terms and conditions of the MIT License (see LICENCE).
*/

#pragma once

#ifndef _AGSIMVEC4_H
#define _AGSIMVEC4_H

#include "plugin/agsplugin.h"
#include "imgui/imgui.h"

struct AgsImVec4 : ImVec4 {
public :
int id;
AgsImVec4(float _x, float _y, float _z, float _w)
: ImVec4(_x,_y,_z,_w){
id = -1;
}

AgsImVec4(float _x, float _y, float _z, float _w, int _id)
: ImVec4(_x,_y,_z,_w){
id = _id;
}
float Length() ;
float SquaredLength();
AgsImVec4* Scale(float scale) ;

AgsImVec4 *Add(AgsImVec4 *agsImVec4);

AgsImVec4 *Sub(AgsImVec4 *agsImVec4);
};

//------------------------------------------------------------------------------
// AGS interface instances

class AgsImVec4Interface : public IAGSScriptManagedObject
{
public:
static const char* name;

AgsImVec4Interface() {};

virtual int Dispose(const char* address, bool force);
virtual const char* GetType() { return (name); }
virtual int Serialize(const char* address, char* buffer, int bufsize);

};

//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

class AgsImVec4Reader : public IAGSManagedObjectReader
{
public:

AgsImVec4Reader() {}

virtual void Unserialize(int key, const char* serializedData, int dataSize);

};

//------------------------------------------------------------------------------

extern AgsImVec4Interface AgsImVec4_Interface;
extern AgsImVec4Reader AgsImVec4_Reader;

//------------------------------------------------------------------------------

#endif /* _AGSIMVEC4_H */

//............

0 comments on commit 1feeea6

Please sign in to comment.