Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hovering over scene objects with ctHoverEvent and ctHoverCamera. #24

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions gamegio-library/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ set(CPP_SRCS
gamegio/MouseItem.cpp
gamegio/KeysItem.cpp
gamegio/ScreenModeItem.cpp
gamegio/HoverItem.cpp
gamegio/HoverCbor.cpp
)

set(CBOR_SRCS
Expand Down
1 change: 1 addition & 0 deletions gamegio-library/src/gamegio/CameraItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "EntityIdCbor.hpp"
#include "ParentCbor.hpp"

#include "Graphics3DSystem.hpp"

using namespace std;

Expand Down
28 changes: 28 additions & 0 deletions gamegio-library/src/gamegio/EntityIdCbor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,34 @@ void writeEntityId(CborEncoder *enc, EntityId entityId)
cbor_encode_byte_string(enc, entityId.data(), entityId.size());
}

void printEID(EntityId eid)
{
for(int j = 0; j < 16; j++)
printf("%02X", eid[j]);
std::cout << "\n";
}

cbd::EntityId BufferToEntityId(PODVector<unsigned char> buf)
{
cbd::EntityId eid;
PODVector<unsigned char>::Iterator element;
for (element = buf.Begin(); element != buf.End(); element++)
{
eid.push_back(*element); //cout << *element;
}
return eid;
}

PODVector<unsigned char> EntityIdToBuffer(cbd::EntityId eid)
{
PODVector<unsigned char> buf; //construct Urho3D buffer
std::vector<uint8_t>::iterator index=eid.begin();
for (int i=0; i<16; i++)
{
buf.Push(*index++);
}
return buf;
}

} // end of namespacd cdb

Expand Down
7 changes: 7 additions & 0 deletions gamegio-library/src/gamegio/EntityIdCbor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
#define __EntityId_cbor__

#include <vector>
#include <iostream>
#include "cbor.h"
#include "cborconstants_p.h"
#include <Urho3D/Urho3DAll.h>


namespace cbd {

Expand All @@ -21,6 +24,10 @@ typedef std::vector<uint8_t> EntityId;
void readEntityId(CborValue *it, EntityId *entityId);
void writeEntityId(CborEncoder *enc, EntityId entityId);

void printEID(EntityId eid);
cbd::EntityId BufferToEntityId(Urho3D::PODVector<unsigned char> buf);
Urho3D::PODVector<unsigned char> EntityIdToBuffer(cbd::EntityId eid);

} // end of namespacd cdb

extern const uint64_t ctEntityId;
Expand Down
16 changes: 15 additions & 1 deletion gamegio-library/src/gamegio/Graphics3DSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ using namespace std;

#include "Fresco.hpp"
#include "Graphics3DSystem.hpp"
#include "EntityIdCbor.hpp"

#include "Graphics3DConfigCbor.hpp"
#include "Graphics3DCommandCbor.hpp"
Expand Down Expand Up @@ -206,14 +207,27 @@ FrItem Graphics3DSystem::msgCreate(FrMsg m, FrMsgLength l)
return (void *)g3ds;
}

void Graphics3DSystem::dumpEntities()
{
std::map<cbd::EntityId, Node*>::iterator i;
for (i = node_map.begin(); i != node_map.end(); i++)
{
cout << "Graphics3DSystem - dumpEntities: EID: ";
printEID(i->first);
cout << "Second: " << i->second << "\n";
//cout << msg;
//URHO3D_LOGRAW(msg + "\n");
}
}

void Graphics3DSystem::msgCommand(FrMsg m, FrMsgLength l)
{
// read message data
CborParser parser; CborValue it;
cbor_parser_init(m, l, 0, &parser, &it);
Graphics3DCommand command;
readGraphics3DCommand(&it, &command);

// dumpEntities();
if (command.selector == Step)
{
if (!engine->IsExiting())
Expand Down
2 changes: 2 additions & 0 deletions gamegio-library/src/gamegio/Graphics3DSystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class Graphics3DSystem {

// messages
void msgCommand(FrMsg m, FrMsgLength l);

void dumpEntities();
};


Expand Down
23 changes: 16 additions & 7 deletions gamegio-library/src/gamegio/HasNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "EntityIdCbor.hpp"
#include "ParentCbor.hpp"
#include "VisibleCbor.hpp"
#include "HoverItem.hpp"

using namespace std;
using namespace cbd;
Expand Down Expand Up @@ -98,13 +99,6 @@ void HasNode::msgScale(FrMsg m, FrMsgLength l)
node->SetScale(Vector3(vec.x, vec.y, vec.z));
};

void printEID(EntityId eid)
{
for(int j = 0; j < 16; j++)
printf("%02X", eid[j]);
cout << "\n";
}

void HasNode::msgParent(FrMsg m, FrMsgLength l)
{
CborParser parser; CborValue it;
Expand Down Expand Up @@ -140,8 +134,23 @@ void HasNode::msgEntityId(FrMsg m, FrMsgLength l)
readEntityId(&it, &eid);
// std::cout << "HasNode - set id: ";
// printEID(eid);
// std::cout << "\n";

Graphics3DSystem::getG3DS()->node_map[eid] = node;

/* Setting the entity ID into the node as a Var
so that it can be retrieved from clicks (raycast) */
// First Convert EID into a Urho3D buffer, PODVector<unsigned char>

PODVector<unsigned char> buf = EntityIdToBuffer(eid);

EntityId duplicateEntity = BufferToEntityId(buf);
// std::cout << "HasNode - Backconverted: ";
// printEID(duplicateEntity);
// std::cout << "\n";

node->SetVar(StringHash(HOVER_ENTITY_HASHKEY), buf);
// cout << "HasNode - SetVar a buf with Size=" << buf.Size() << "\n";
}

void HasNode::msgVisible(FrMsg m, FrMsgLength l)
Expand Down
37 changes: 37 additions & 0 deletions gamegio-library/src/gamegio/HoverCbor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "HoverCbor.hpp"

namespace cbd {

void readHoverEvent(CborValue *it0, HoverEvent *hoverEvent)
{
CborValue it1; CborValue *it = &it1; cbor_value_enter_container(it0, it);
int i; cbor_value_get_int(it, &i); cbor_value_advance_fixed(it);
hoverEvent->selector = (EnumMaybeEntity)i;
if (hoverEvent->selector == 0) {
};
if (hoverEvent->selector == 1) {
readEntityId(it, &(hoverEvent->data.HoverEntity.value0));
};
cbor_value_leave_container(it0, it);
}


void writeHoverEvent(CborEncoder *enc0, HoverEvent hoverEvent)
{
if (hoverEvent.selector == 0) {
CborEncoder enc1; CborEncoder* enc = &enc1; cbor_encoder_create_array(enc0, enc, 1);
cbor_encode_uint(enc, (uint64_t)hoverEvent.selector);
cbor_encoder_close_container_checked(enc0, enc);
};
if (hoverEvent.selector == 1) {
CborEncoder enc1; CborEncoder* enc = &enc1; cbor_encoder_create_array(enc0, enc, 2);
cbor_encode_uint(enc, (uint64_t)hoverEvent.selector);
writeEntityId(enc, hoverEvent.data.HoverEntity.value0);
cbor_encoder_close_container_checked(enc0, enc);
};
}

} // end of namespace cdb

const uint64_t ctHoverCamera = 0xc9947e814b1f9b89;
const uint64_t ctHoverEvent = 0x76492fa6aa357311;
32 changes: 32 additions & 0 deletions gamegio-library/src/gamegio/HoverCbor.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef __Hover_cbor__
#define __Hover_cbor__

#include "EntityIdCbor.hpp"

namespace cbd {

typedef enum {
NoEntity = 0,
JustEntity = 1,
} EnumMaybeEntity;

typedef struct {
EnumMaybeEntity selector;
struct {
struct {
EntityId value0;
} HoverEntity;
} data;
} HoverEvent;


void readHoverEvent(CborValue *it0, HoverEvent *hoverEvent);
void writeHoverEvent(CborEncoder *enc0, HoverEvent hoverEvent);

}

extern const uint64_t ctHoverCamera;
extern const uint64_t ctHoverEvent;

#endif