From 24854a062acec3cb2a420686392ba0a6dd39edc6 Mon Sep 17 00:00:00 2001 From: andreja6 Date: Fri, 13 Mar 2020 16:28:12 -0700 Subject: [PATCH] Added tools and texture handler --- Application.cpp | 82 ++++++---------------------- Application.h | 4 +- ArrowTool.cpp | 28 ++++++++++ ArrowTool.h | 13 +++++ G3DTest.vcproj | 16 ++++++ ModeSelectionListener.cpp | 3 +- Mouse.cpp | 65 ++++++++++++++++++++-- Mouse.h | 8 ++- TextureHandler.cpp | 12 +++++ TextureHandler.h | 87 ++++++++++++++++++++++++++++++ Tool.cpp | 13 ++--- Tool.h | 34 ++++++------ content/images/ArrowFarCursor.png | Bin 0 -> 2169 bytes 13 files changed, 267 insertions(+), 98 deletions(-) create mode 100644 ArrowTool.cpp create mode 100644 ArrowTool.h create mode 100644 TextureHandler.cpp create mode 100644 TextureHandler.h create mode 100644 content/images/ArrowFarCursor.png diff --git a/Application.cpp b/Application.cpp index 4a8ffaa..c643ecc 100644 --- a/Application.cpp +++ b/Application.cpp @@ -30,8 +30,8 @@ #include "DeleteListener.h" #include "CameraButtonListener.h" #include "RotateButtonListener.h" -#define LEGACY_LOAD_G3DFUN_LEVEL -Ray testRay; +//#define LEGACY_LOAD_G3DFUN_LEVEL +//Ray testRay; //static int cursorid = 0; //static int cursorOvrid = 0; //static int currentcursorid = 0; @@ -161,8 +161,7 @@ void Application::deleteInstance() { AudioPlayer::playSound(GetFileInPath("/content/sounds/pageturn.wav")); Instance* selectedInstance = g_selectedInstances.at(0); - if(selectedInstance->getParent() != NULL) - selectedInstance->getParent()->removeChild(selectedInstance); + selectedInstance->setParent(NULL); delete selectedInstance; selectedInstance = NULL; g_selectedInstances.erase(g_selectedInstances.begin()); @@ -415,7 +414,11 @@ bool IsHolding(int button) */ void Application::onUserInput(UserInput* ui) { - + if(mouseMoveState) + { + mouseMoveState = false; + tool->onMouseMoved(mouse); + } /* if(GetHoldKeyState(VK_LCONTROL)) { @@ -441,7 +444,7 @@ void Application::onUserInput(UserInput* ui) { mouse.setMouseDown((GetKeyState(VK_LBUTTON) & 0x100) != 0); if (GetHoldKeyState(VK_LBUTTON)) { - if (_dragging) { + /* if (_dragging) { PartInstance* part = NULL; if(g_selectedInstances.size() > 0) part = (PartInstance*) g_selectedInstances.at(0); @@ -466,7 +469,7 @@ void Application::onUserInput(UserInput* ui) { } } Sleep(10); - } + }*/ } // Camera KB Handling { if (GetKPBool(VK_OEM_COMMA)) //Left @@ -485,11 +488,14 @@ void Application::onUserInput(UserInput* ui) { void Application::changeTool(Tool * newTool) { + tool->onDeselect(mouse); delete tool; if(newTool != NULL) tool = newTool; else tool = new Tool(); //Nulltool + tool->onSelect(mouse); + } void Application::makeFlag(Vector3 &vec, RenderDevice* &rd) @@ -795,63 +801,6 @@ void Application::onMouseLeftPressed(HWND hwnd,int x,int y) if(!onGUI) { tool->onButton1MouseDown(mouse); - Instance * selectedInstance = NULL; - testRay = cameraController.getCamera()->worldRay(mouse.x, mouse.y, renderDevice->getViewport()); - float nearest=std::numeric_limits::infinity(); - Vector3 camPos = cameraController.getCamera()->getCoordinateFrame().translation; - std::vector instances = _dataModel->getWorkspace()->getAllChildren(); - bool objFound = false; - for(size_t i = 0; i < instances.size(); i++) - { - if(PartInstance* test = dynamic_cast(instances.at(i))) - { - float time = testRay.intersectionTime(test->getBox()); - - if (time != inf()) - { - objFound = true; - if (nearest>time) - { - nearest=time; - //bool found = false; - selectedInstance = test; - /*for(size_t i = 0; i < g_selectedInstances.size(); i++) - { - if(g_selectedInstances.at(i) == test) - { - found = true; - //ShowWindow(_propWindow->_hwndProp, SW_SHOW); - //SetActiveWindow(_propWindow->_hwndProp); - //SetForegroundWindow(_propWindow->_hwndProp); - break; - } - } - if(!found) - { - selectedInstance = test; - //if(!GetHoldKeyState(VK_RCONTROL) && !GetHoldKeyState(VK_LCONTROL)) - //g_selectedInstances.clear(); - //if(std::find(g_selectedInstances.begin(), g_selectedInstances.end(),test)==g_selectedInstances.end()) - //g_selectedInstances.push_back(test); - } - //selectInstance(test, _propWindow); - //_message = "Dragging = true."; - //_messageTime = System::time(); - //_dragging = true;*/ - } - } - } - } - if(!objFound) - selectInstance(_dataModel,_propWindow); - else - { - while(selectedInstance->getParent() != g_dataModel->getWorkspace()) - { - selectedInstance = selectedInstance->getParent(); - } - selectInstance(selectedInstance, _propWindow); - } } } @@ -895,9 +844,12 @@ void Application::onMouseRightUp(int x,int y) void Application::onMouseMoved(int x,int y) { oldMouse = Vector2(mouse.x, mouse.y); + mouse.oldx = mouse.x; + mouse.oldy = mouse.y; mouse.x = x; mouse.y = y; - tool->onMouseMoved(mouse); + //tool->onMouseMoved(mouse); + mouseMoveState = true; //_dataModel->mousex = x; //_dataModel->mousey = y; diff --git a/Application.h b/Application.h index ced42a1..c33f121 100644 --- a/Application.h +++ b/Application.h @@ -56,10 +56,12 @@ class Application { // : public GApp { RenderDevice* getRenderDevice(); void selectInstance(Instance* selectedInstance,PropertyWindow* propWindow); void setMode(int mode); - Mouse mouse; + Tool * tool; void changeTool(Tool *); + Mouse mouse; private: + bool mouseMoveState; RenderDevice* renderDevice; //void initGUI(); HWND _hWndMain; diff --git a/ArrowTool.cpp b/ArrowTool.cpp new file mode 100644 index 0000000..784d57f --- /dev/null +++ b/ArrowTool.cpp @@ -0,0 +1,28 @@ +#include "ArrowTool.h" + +ArrowTool::ArrowTool(void) +{ +} + +ArrowTool::~ArrowTool(void) +{ +} + +void ArrowTool::onButton1MouseDown(Mouse mouse) +{ + g_selectedInstances.clear(); + PartInstance * target = mouse.getTarget(); + if(target != NULL) + g_selectedInstances.push_back(target); +} +void ArrowTool::onMouseMoved(Mouse mouse) +{ + PartInstance * target = mouse.getTarget(); + if(target == NULL) + this->setCursor(GetFileInPath("/content/images/ArrowCursor.png")); + else this->setCursor(GetFileInPath("/content/images/DragCursor.png")); +} +void ArrowTool::onSelect(Mouse mouse) +{ + this->setCursor(GetFileInPath("/content/images/ArrowCursor.png")); +} \ No newline at end of file diff --git a/ArrowTool.h b/ArrowTool.h new file mode 100644 index 0000000..7f24e0b --- /dev/null +++ b/ArrowTool.h @@ -0,0 +1,13 @@ +#pragma once +#include "tool.h" + +class ArrowTool : + public Tool +{ +public: + ArrowTool(void); //OnSelect? + ~ArrowTool(void); //OnDeselect? + virtual void onButton1MouseDown(Mouse); + virtual void onMouseMoved(Mouse mouse); + virtual void onSelect(Mouse mouse); //Why is this not being called +}; diff --git a/G3DTest.vcproj b/G3DTest.vcproj index d2d994b..036c122 100644 --- a/G3DTest.vcproj +++ b/G3DTest.vcproj @@ -306,6 +306,10 @@ RelativePath=".\StringFunctions.cpp" > + + @@ -401,6 +405,10 @@ + + @@ -503,6 +511,10 @@ RelativePath=".\StringFunctions.h" > + + @@ -594,6 +606,10 @@ + + diff --git a/ModeSelectionListener.cpp b/ModeSelectionListener.cpp index 653cd8e..9356155 100644 --- a/ModeSelectionListener.cpp +++ b/ModeSelectionListener.cpp @@ -2,6 +2,7 @@ #include "Instance.h" #include "ModeSelectionListener.h" #include "Application.h" +#include "ArrowTool.h" void ModeSelectionListener::onButton1MouseClick(BaseButtonInstance* button) { @@ -20,7 +21,7 @@ void ModeSelectionListener::onButton1MouseClick(BaseButtonInstance* button) button->selected = true; if(button->name == "Cursor") - g_usableApp->setMode(CURSOR); + g_usableApp->changeTool(new ArrowTool()); else if(button->name == "Resize") g_usableApp->setMode(RESIZE); else if(button->name == "Arrows") diff --git a/Mouse.cpp b/Mouse.cpp index 4b9cd8a..b903b43 100644 --- a/Mouse.cpp +++ b/Mouse.cpp @@ -1,14 +1,43 @@ #include "Mouse.h" +#include "Application.h" +#include "Globals.h" Mouse::Mouse(){ x = y = 0; } Mouse::~Mouse(){} - -Instance * Mouse::getTarget() +PartInstance * selectedInstance = NULL; +Ray testRay; +float nearest=std::numeric_limits::infinity(); +void eprt(PartInstance * instance) { - return NULL; + float time = testRay.intersectionTime(instance->getBox()); + if (time != inf()) + { + if (nearest>time) + { + nearest=time; + selectedInstance = instance; + //This is where dead code below was + } + } } + +PartInstance * Mouse::getTarget() +{ + selectedInstance = NULL; + testRay = g_usableApp->cameraController.getCamera()->worldRay(x, y, g_usableApp->getRenderDevice()->getViewport()); + nearest=std::numeric_limits::infinity(); + //Vector3 camPos = g_usableApp->cameraController.getCamera()->getCoordinateFrame().translation; + for_each (g_dataModel->getWorkspace()->partObjects.begin(), g_dataModel->getWorkspace()->partObjects.end(), eprt); + return selectedInstance; +} + + + + + + Vector3 Mouse::getPosition() { return Vector3(); @@ -28,4 +57,32 @@ bool Mouse::isMouseDown() void Mouse::setMouseDown(bool bval) { mouseDown = bval; -} \ No newline at end of file +} + + + + //bool found = false; + /*for(size_t i = 0; i < g_selectedInstances.size(); i++) + { + if(g_selectedInstances.at(i) == test) + { + found = true; + //ShowWindow(_propWindow->_hwndProp, SW_SHOW); + //SetActiveWindow(_propWindow->_hwndProp); + //SetForegroundWindow(_propWindow->_hwndProp); + break; + } + } + if(!found) + { + selectedInstance = test; + //if(!GetHoldKeyState(VK_RCONTROL) && !GetHoldKeyState(VK_LCONTROL)) + //g_selectedInstances.clear(); + //if(std::find(g_selectedInstances.begin(), g_selectedInstances.end(),test)==g_selectedInstances.end()) + //g_selectedInstances.push_back(test); + } + //selectInstance(test, _propWindow); + //_message = "Dragging = true."; + //_messageTime = System::time(); + //_dragging = true;*/ + diff --git a/Mouse.h b/Mouse.h index 83a1c4c..ff7d1d8 100644 --- a/Mouse.h +++ b/Mouse.h @@ -1,14 +1,18 @@ #pragma once #include "PartInstance.h" +#pragma once +#include "WorkspaceInstance.h" + #pragma once class Mouse { public: - Mouse(void); + Mouse(); ~Mouse(void); int x, y; - Instance * getTarget(); + int oldx, oldy; + PartInstance * getTarget(); Vector3 getPosition(); bool isMouseOnScreen(); bool isMouseDown(); diff --git a/TextureHandler.cpp b/TextureHandler.cpp new file mode 100644 index 0000000..9e3a578 --- /dev/null +++ b/TextureHandler.cpp @@ -0,0 +1,12 @@ +#include "TextureHandler.h" + +TextureHandler::TextureHandler(void) +{ +} + +TextureHandler::~TextureHandler(void) +{ +} +std::map TextureHandler::volTex = std::map(); +std::map TextureHandler::volExp = std::map(); +std::map TextureHandler::permTex = std::map(); \ No newline at end of file diff --git a/TextureHandler.h b/TextureHandler.h new file mode 100644 index 0000000..0c0abf9 --- /dev/null +++ b/TextureHandler.h @@ -0,0 +1,87 @@ +#pragma once +#include +#include +class TextureHandler +{ +public: + TextureHandler(void); + ~TextureHandler(void); + static int getTextureVolatile(std::string str) + { + if(!volTex[str].isNull()) + { + volExp[str] = G3D::System::time(); + return volTex[str]->openGLID(); + } + else + { + G3D::TextureRef tex = G3D::Texture::fromFile(str); + volExp[str] = G3D::System::time(); + volTex[str] = tex; + return tex->openGLID(); + } + } + static int getTexturePermanent(std::string str) + { + if(!permTex[str].isNull()) + { + return permTex[str]->openGLID(); + } + else + { + G3D::TextureRef tex = G3D::Texture::fromFile(str); + permTex[str] = tex; + return tex->openGLID(); + } + } + static void flushVolatile() + { + std::map::iterator it; + for (it=volExp.begin(); it!=volExp.end(); ++it) + { + if(it->second < G3D::System::time() - 3) + { + volTex[it->first]->~Texture(); + volTex.erase(it->first); + volExp.erase(it->first); + //it->second->~ReferenceCountedPointer(); + } + } + } + static void dispose() + { + std::map::iterator it; + for (it=volTex.begin(); it!=volTex.end(); ++it) + { + it->second->~Texture(); + //it->second->~ReferenceCountedPointer(); + } + for (it=permTex.begin(); it!=permTex.end(); ++it) + { + it->second->~Texture(); + //it->second->~ReferenceCountedPointer(); + } + permTex.clear(); + volTex.clear(); + } + + static void remove(std::string str) + { + if(!volTex[str].isNull()) + { + volTex[str]->~Texture(); + //volTex[str]->~ReferenceCountedPointer(); + volTex.erase(str); + } + if(!permTex[str].isNull()) + { + permTex[str]->~Texture(); + //permTex[str]->~ReferenceCountedPointer(); + permTex.erase(str); + } + } +private: + static std::map volTex; + static std::map volExp; + static std::map permTex; +}; \ No newline at end of file diff --git a/Tool.cpp b/Tool.cpp index 169b92b..529f3af 100644 --- a/Tool.cpp +++ b/Tool.cpp @@ -2,8 +2,8 @@ Tool::Tool(void) { - cursorRef = Texture::fromFile(GetFileInPath("/content/images/ArrowCursor.png")); - cursorId = cursorRef->openGLID(); + cursorString = GetFileInPath("/content/images/ArrowFarCursor.png"); + //cursorId = cursorRef->openGLID(); } Tool::~Tool(void) @@ -22,14 +22,11 @@ void Tool::onDeselect(Mouse mouse){} void Tool::onMouseScroll(Mouse mouse){} void Tool::onKeyDown(char key){} void Tool::onKeyUp(char key){} -void Tool::setCursor(std::string) +void Tool::setCursor(std::string str) { + cursorString = str; } int Tool::getCursorId() { - return cursorId; -} -void Tool::setCursorId(int id) -{ - cursorId = id; + return TextureHandler::getTexturePermanent(cursorString); } \ No newline at end of file diff --git a/Tool.h b/Tool.h index bd1868b..dc81f7d 100644 --- a/Tool.h +++ b/Tool.h @@ -1,4 +1,6 @@ #pragma once +#include "TextureHandler.h" +#pragma once #include #pragma once #include "Mouse.h" @@ -12,23 +14,21 @@ class Tool public: Tool(void); ~Tool(void); - void onButton1MouseDown(Mouse); //yes - void onButton2MouseDown(Mouse); //yes - void onButton3MouseDown(Mouse); - void onButton1MouseUp(Mouse);//yes - void onButton2MouseUp(Mouse);//yes - void onButton3MouseUp(Mouse); - void onMouseMoved(Mouse);//yes - void onSelect(Mouse);//For later - void onDeselect(Mouse);//For later - void onMouseScroll(Mouse);//Kinda - void onKeyDown(char);//yes - void onKeyUp(char);//yes - void setCursor(std::string); - void setCursorId(int);//yes - int getCursorId();//yes + virtual void onButton1MouseDown(Mouse); //yes + virtual void onButton2MouseDown(Mouse); //yes + virtual void onButton3MouseDown(Mouse); //no + virtual void onButton1MouseUp(Mouse);//yes + virtual void onButton2MouseUp(Mouse);//yes + virtual void onButton3MouseUp(Mouse); //no + virtual void onMouseMoved(Mouse);//yes + virtual void onSelect(Mouse);//yes + virtual void onDeselect(Mouse);//yes + virtual void onMouseScroll(Mouse);//Kinda + virtual void onKeyDown(char);//yes + virtual void onKeyUp(char);//yes + virtual int getCursorId();//yes +protected: + virtual void setCursor(std::string); private: - int cursorId; - G3D::TextureRef cursorRef; std::string cursorString; }; diff --git a/content/images/ArrowFarCursor.png b/content/images/ArrowFarCursor.png new file mode 100644 index 0000000000000000000000000000000000000000..9a1954a334a7b330ad6beae16d11c14ef670f0a6 GIT binary patch literal 2169 zcmb7F`8yMiA9uDKi_8$JIaY3SPAILAOmY;V^wpp6eLv6Z{d%6)^Ss`#=Xw3|PDa^W0t!kCa&d71krt+Q2aNiM!-o!b z`q7Q*15zWIp-C4*ys;#F;0=faKERvH0T&9<)`r-72SK!9Fg=KtwxKS}PzMfChZ(?N zwQnsJ4}1U;(%K9#!Yw2r!{;Ec>Tw_m1X(x}xwuYr`~!Cv-ogGrSY16@LF+oaSgb4);Jk_6gkjP{!bg|pz%PGOkLPT40bMx1{hYxdyaHQFM zz}&blELQ+}mu^>8B1Kr{@Vm6|;A0*m&VHe0W@2)(vW`lhJZUoY=~HV>X=!)i5RNf1 zKE7go>Yj*KF%?A?201hM8NIJXv>*tL9d1x?x-Atp6Pu!>q9Qoa5HMkzm6f$n+xKvO zHY_a6XZwczs*!Nntr|k0IS^(1xMUrG8z#^vySg4vybSuY`?CU%$9E464K*eSAM12; zbyf3(DsL)MdlU3pp8LwGvP`9ZodpG_1x_(`FwV|nyztmxYct`E{$n9Z%E}ew$)@0K z27{qieYdZmtv#?EP7NX&(cz=)!k2-dUvOn?M;w1NtMclNv(0r)O=O~@ zfNEr9^bm~fOMEe6jsCL{E6DjJh)7&tE+p{lW}q|COW#Q zwY9bMzkA*%Fvb4MITFa=+Eo3^t5Tw(qFV+cXM$%{ni7| zElg{yiIQ93gD=4RAIdbeugLn@K04RGyu2JK4(LbMdnJ3~@n0z^DJcSUI(-rYlUo0I zHOK_bRp8kvwg;8%h`@kx(q}>xGg4Bd+4p30baZGNo11=+&nxruUSZC=a)KSYs5iU8 zJawkBXubw}d_A@+$qfpHo)c-o&h?~q%`7fZD3k|dIguRyAOn!edr-#%?IYbrA#-6V z?$*}UfnDJEmmoDwO}4_6ncVdr7Ml&w)7Q__%$hjCN9-VMiMx)BM`10Qi?=Ct`9w*Q zqVU-uZf(w0OF6I6TKgw9F%df(3uF8H`z=?tWVe6($d8`u0fzq=n6JE;d*@G1ZaM#> za@z(d|6Mvc{@Dde!5v>|^lZUF1C~A%Dpe}hYgEBP`<{xbYN(u?T=2&ywttTVd_pK~ z&?_rd*o%uk1pW%zW1iNeWIooCy^LRn)S41bSWs{lH6TxtIl>vK{#g9VFO*2sA?W7C za*XaKXJoirz*i?416M|>uT5l=Ed&+XrrwzP$IPN}s*tBUvMWL1;Y*e|8Vi2xntoRX zIan{oQQY&ywtbOrN^0snacO{tfJ=hG5nyrgwWA8=RB!yG*~h#JUbEyEy*nkcgzo9$ z=no}GS*-#8&=TFtV!EM4yEXhFW%uukr>ExEhku_MD9NQ#4GiVwI}4Sv_6vMG(hVS@ z{igb>4tcZn!|y(1-(s8;t+?=(F|0vSoQa{XO zq6%m-o+^Ax)LjH@#Zf*->$5lSE0-lI*#&_eH!r?RwR%Eg(QQe*J*)`HYZq#4%zP zbc&nUE=LG#c*d03E*^+aDy6mR_KSnW#3~)D&_x`TCeJtN=6$OxZB?&wetKH20Q0F9 zV%dcP)sW*N`3O7+tDJDcr6p!>D(n{_#G!+1H~NF)c5Et(k+c0cLCnoj=8yXtm91iEJ!q0h-!Jdv83NL%AI!Fotxi?28#5ZuS>pR=Ai z4cb~+F{+ztkXbu|CA`&?mDX*_cn8fY;_scVz1M4NVVhBUCB`A!B&PkhZWq-r z*)yIP0Pw_p`8