Browse Source

feat: implement key/mouse event processing

Sam Jaffe 1 year ago
parent
commit
6cac04c990

+ 69 - 2
engine/engine.xcodeproj/project.pbxproj

@@ -9,7 +9,6 @@
 /* Begin PBXBuildFile section */
 		CD1C83562298B55F00825C4E /* fps_counter.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD1C83542298B55F00825C4E /* fps_counter.cxx */; };
 		CD1C8419229A095600825C4E /* text_engine.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD1C8417229A095600825C4E /* text_engine.cxx */; };
-		CD210444285E4CBC00F15770 /* libjsoncpp_static.a in Frameworks */ = {isa = PBXBuildFile; fileRef = CD210443285E4CB100F15770 /* libjsoncpp_static.a */; };
 		CD39A88F22F521DB00FEC384 /* libjsoncpp.1.9.0.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = CD39A88D22F521A200FEC384 /* libjsoncpp.1.9.0.dylib */; };
 		CD39A89222F6052D00FEC384 /* entity_test.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD39A89122F6052D00FEC384 /* entity_test.cxx */; };
 		CD62D8462251A94C0023219A /* libgraphics.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = CD62D8452251A94C0023219A /* libgraphics.dylib */; };
@@ -27,6 +26,10 @@
 		CD8064FB22D69CAE00B9B4E4 /* game_dispatch_test.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD8064FA22D69CAE00B9B4E4 /* game_dispatch_test.cxx */; };
 		CD8064FD22D9456100B9B4E4 /* scene_test.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD8064FC22D9456100B9B4E4 /* scene_test.cxx */; };
 		CD8064FE22DA621200B9B4E4 /* libmath.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = CD62D8472251A9500023219A /* libmath.dylib */; };
+		CD8FBC8D2B743A8900CE38F1 /* game in Headers */ = {isa = PBXBuildFile; fileRef = CDA34D8422515C99008036A7 /* game */; settings = {ATTRIBUTES = (Public, ); }; };
+		CD9F1BA42B74428E0035EDF5 /* KeyBindingView.mm in Sources */ = {isa = PBXBuildFile; fileRef = CD9F1BA32B74428E0035EDF5 /* KeyBindingView.mm */; };
+		CD9F1BAA2B7446A80035EDF5 /* libjsoncpp.a in Frameworks */ = {isa = PBXBuildFile; fileRef = CD9F1BA92B7446A80035EDF5 /* libjsoncpp.a */; };
+		CD9F1BB02B7447080035EDF5 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CD9F1BAF2B7447080035EDF5 /* Cocoa.framework */; };
 		CDB1F8C81D7A312B00700C6B /* game_dispatch.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CDB1F8C61D7A312B00700C6B /* game_dispatch.cpp */; settings = {COMPILER_FLAGS = "-fvisibility=default"; }; };
 		CDB1F8CC1D7A319A00700C6B /* scene.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CDB1F8CA1D7A319A00700C6B /* scene.cpp */; settings = {COMPILER_FLAGS = "-fvisibility=default"; }; };
 		CDB1F8D21D7A32A300700C6B /* events.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CDB1F8D01D7A32A300700C6B /* events.cpp */; settings = {COMPILER_FLAGS = "-fvisibility=default"; }; };
@@ -98,6 +101,20 @@
 		CD8064F822D2984400B9B4E4 /* fps_counter_test.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = fps_counter_test.cxx; sourceTree = "<group>"; };
 		CD8064FA22D69CAE00B9B4E4 /* game_dispatch_test.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = game_dispatch_test.cxx; sourceTree = "<group>"; };
 		CD8064FC22D9456100B9B4E4 /* scene_test.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = scene_test.cxx; sourceTree = "<group>"; };
+		CD8FBC822B743A7D00CE38F1 /* engine_fwd.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = engine_fwd.hpp; sourceTree = "<group>"; };
+		CD8FBC832B743A7D00CE38F1 /* serial.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = serial.hpp; sourceTree = "<group>"; };
+		CD8FBC842B743A7D00CE38F1 /* events.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = events.hpp; sourceTree = "<group>"; };
+		CD8FBC852B743A7D00CE38F1 /* game_dispatch.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = game_dispatch.hpp; sourceTree = "<group>"; };
+		CD8FBC862B743A7D00CE38F1 /* fps_counter.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = fps_counter.hpp; sourceTree = "<group>"; };
+		CD8FBC872B743A7D00CE38F1 /* scene.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = scene.hpp; sourceTree = "<group>"; };
+		CD8FBC882B743A7D00CE38F1 /* text_engine.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = text_engine.hpp; sourceTree = "<group>"; };
+		CD8FBC892B743A7D00CE38F1 /* entity.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = entity.hpp; sourceTree = "<group>"; };
+		CD9F1BA32B74428E0035EDF5 /* KeyBindingView.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = KeyBindingView.mm; sourceTree = "<group>"; };
+		CD9F1BA82B7442FE0035EDF5 /* KeyBindingView.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = KeyBindingView.h; sourceTree = "<group>"; };
+		CD9F1BA92B7446A80035EDF5 /* libjsoncpp.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libjsoncpp.a; path = ../../../../../../../opt/local/lib/libjsoncpp.a; sourceTree = "<group>"; };
+		CD9F1BAB2B7446E10035EDF5 /* AVFoundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AVFoundation.framework; path = System/Library/Frameworks/AVFoundation.framework; sourceTree = SDKROOT; };
+		CD9F1BAD2B7446EC0035EDF5 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
+		CD9F1BAF2B7447080035EDF5 /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = System/Library/Frameworks/Cocoa.framework; sourceTree = SDKROOT; };
 		CDA34D8422515C99008036A7 /* game */ = {isa = PBXFileReference; lastKnownFileType = folder; name = game; path = include/game; sourceTree = "<group>"; };
 		CDB1F8AE1D7A30CD00700C6B /* libengine.dylib */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.dylib"; includeInIndex = 0; path = libengine.dylib; sourceTree = BUILT_PRODUCTS_DIR; };
 		CDB1F8C61D7A312B00700C6B /* game_dispatch.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = game_dispatch.cpp; sourceTree = "<group>"; };
@@ -125,10 +142,11 @@
 			isa = PBXFrameworksBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
+				CD9F1BB02B7447080035EDF5 /* Cocoa.framework in Frameworks */,
 				CD7E87792295FAB400D877FE /* libgameutils.dylib in Frameworks */,
-				CD210444285E4CBC00F15770 /* libjsoncpp_static.a in Frameworks */,
 				CD62D8482251A9500023219A /* libmath.dylib in Frameworks */,
 				CD62D8462251A94C0023219A /* libgraphics.dylib in Frameworks */,
+				CD9F1BAA2B7446A80035EDF5 /* libjsoncpp.a in Frameworks */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};
@@ -138,6 +156,10 @@
 		CD62D8442251A94C0023219A /* Frameworks */ = {
 			isa = PBXGroup;
 			children = (
+				CD9F1BAF2B7447080035EDF5 /* Cocoa.framework */,
+				CD9F1BAD2B7446EC0035EDF5 /* Foundation.framework */,
+				CD9F1BAB2B7446E10035EDF5 /* AVFoundation.framework */,
+				CD9F1BA92B7446A80035EDF5 /* libjsoncpp.a */,
 				CD210443285E4CB100F15770 /* libjsoncpp_static.a */,
 				CD39A88D22F521A200FEC384 /* libjsoncpp.1.9.0.dylib */,
 				CD7E87782295FAB400D877FE /* libgameutils.dylib */,
@@ -167,6 +189,47 @@
 			path = "engine-test";
 			sourceTree = "<group>";
 		};
+		CD8FBC7F2B743A7D00CE38F1 /* include */ = {
+			isa = PBXGroup;
+			children = (
+				CD8FBC802B743A7D00CE38F1 /* game */,
+			);
+			path = include;
+			sourceTree = "<group>";
+		};
+		CD8FBC802B743A7D00CE38F1 /* game */ = {
+			isa = PBXGroup;
+			children = (
+				CD9F1BA72B7442FE0035EDF5 /* osx */,
+				CD8FBC812B743A7D00CE38F1 /* engine */,
+			);
+			path = game;
+			sourceTree = "<group>";
+		};
+		CD8FBC812B743A7D00CE38F1 /* engine */ = {
+			isa = PBXGroup;
+			children = (
+				CD8FBC822B743A7D00CE38F1 /* engine_fwd.hpp */,
+				CD8FBC832B743A7D00CE38F1 /* serial.hpp */,
+				CD8FBC842B743A7D00CE38F1 /* events.hpp */,
+				CD8FBC852B743A7D00CE38F1 /* game_dispatch.hpp */,
+				CD8FBC862B743A7D00CE38F1 /* fps_counter.hpp */,
+				CD8FBC872B743A7D00CE38F1 /* scene.hpp */,
+				CD8FBC882B743A7D00CE38F1 /* text_engine.hpp */,
+				CD8FBC892B743A7D00CE38F1 /* entity.hpp */,
+			);
+			path = engine;
+			sourceTree = "<group>";
+		};
+		CD9F1BA72B7442FE0035EDF5 /* osx */ = {
+			isa = PBXGroup;
+			children = (
+				CD9F1BA82B7442FE0035EDF5 /* KeyBindingView.h */,
+			);
+			name = osx;
+			path = include/game/osx;
+			sourceTree = SOURCE_ROOT;
+		};
 		CDA34D8522515CA9008036A7 /* test */ = {
 			isa = PBXGroup;
 			children = (
@@ -187,6 +250,7 @@
 			children = (
 				CD62FCB622904A7B00376440 /* GoogleMock.xcodeproj */,
 				CDA34D8422515C99008036A7 /* game */,
+				CD8FBC7F2B743A7D00CE38F1 /* include */,
 				CDB1F8B01D7A30CD00700C6B /* src */,
 				CDA34D8522515CA9008036A7 /* test */,
 				CD62FCCA22904A8900376440 /* engine-test */,
@@ -207,6 +271,7 @@
 		CDB1F8B01D7A30CD00700C6B /* src */ = {
 			isa = PBXGroup;
 			children = (
+				CD9F1BA32B74428E0035EDF5 /* KeyBindingView.mm */,
 				CD62FD33229364DB00376440 /* entity.cxx */,
 				CDB1F8D01D7A32A300700C6B /* events.cpp */,
 				CDB1F8C61D7A312B00700C6B /* game_dispatch.cpp */,
@@ -225,6 +290,7 @@
 			isa = PBXHeadersBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
+				CD8FBC8D2B743A8900CE38F1 /* game in Headers */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};
@@ -392,6 +458,7 @@
 			isa = PBXSourcesBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
+				CD9F1BA42B74428E0035EDF5 /* KeyBindingView.mm in Sources */,
 				CD62FD35229364DB00376440 /* entity.cxx in Sources */,
 				CDB1F8D21D7A32A300700C6B /* events.cpp in Sources */,
 				CD1C83562298B55F00825C4E /* fps_counter.cxx in Sources */,

+ 11 - 1
engine/include/game/engine/game_dispatch.hpp

@@ -51,12 +51,22 @@ private:
   env::clock::tick next_frame();
 
 private:
+  // A value representing the "effective screen size"
   math::vec2 screen_size_;
-  std::shared_ptr<graphics::renderer> renderer_, batch_renderer_;
+
+  std::shared_ptr<graphics::renderer> renderer_;
+  std::shared_ptr<graphics::renderer> batch_renderer_;
+
+  // Minimum frame duration: e.g. 0 - Uncapped, 1/60s - 60fps, etc.
   env::clock::duration minimum_frame_duration_;
+
+  // An engine for rendering text boxes
   std::shared_ptr<text_engine> text_engine_;
+
+  // An engine for displaying the game FPS
   std::unique_ptr<fps_counter> counter_;
 
+  // A mapping of scenes - where user interaction/state lives
   std::unordered_map<scene_id_t, scene_t> scenes_;
 
   bool running = true;

+ 19 - 0
engine/include/game/osx/KeyBindingView.h

@@ -0,0 +1,19 @@
+//
+//  KeyBindingView.h
+//  gameutils
+//
+//  Created by Sam Jaffe on 2/7/24.
+//  Copyright © 2024 Sam Jaffe. All rights reserved.
+//
+
+#import <Cocoa/Cocoa.h>
+
+#include "game/engine/engine_fwd.hpp"
+
+NS_ASSUME_NONNULL_BEGIN
+
+@interface KeyBindingView : NSView
+- (engine::game_dispatch*) engine;
+@end
+
+NS_ASSUME_NONNULL_END

+ 57 - 0
engine/src/KeyBindingView.mm

@@ -0,0 +1,57 @@
+//
+//  KeyBindingView.mm
+//  gameutils
+//
+//  Created by Sam Jaffe on 2/7/24.
+//  Copyright © 2024 Sam Jaffe. All rights reserved.
+//
+
+#import "game/osx/KeyBindingView.h"
+
+#include <array>
+
+#include "game/engine/events.hpp"
+#include "game/engine/game_dispatch.hpp"
+#include "vector/vector.hpp"
+
+static math::vec2 cast(NSPoint point) {
+  return {{static_cast<float>(point.x), static_cast<float>(point.y)}};
+}
+
+@implementation KeyBindingView
+
+NSEventModifierFlags previousModifierFlags = 0;
+
+-(void)mouseUp:(NSEvent *)event {
+  [self engine]->process_mouse_event(cast(NSEvent.mouseLocation), false);
+}
+
+-(void)mouseDown:(NSEvent *)event {
+  [self engine]->process_mouse_event(cast(NSEvent.mouseLocation), true);
+}
+
+-(void)flagsChanged:(NSEvent *)event {
+  std::array<std::pair<NSEventModifierFlags, engine::key_enum_t>, 4> flags{
+    std::pair{NSEventModifierFlagShift, engine::keys::MOD_SHIFT},
+    {NSEventModifierFlagControl, engine::keys::MOD_CONTROL},
+    {NSEventModifierFlagOption, engine::keys::MOD_ALT},
+    {NSEventModifierFlagCommand, engine::keys::MOD_COMMAND}};
+  for (auto [flag, key] : flags) {
+    bool const is_on = event.modifierFlags & flag;
+    if (is_on == (previousModifierFlags & flag)) {
+      continue;
+    }
+    [self engine]->process_key_event(key, is_on);
+  }
+  previousModifierFlags = event.modifierFlags;
+}
+
+-(void)keyUp:(NSEvent *)event {
+  [self engine]->process_key_event(event.keyCode, false);
+}
+
+-(void)keyDown:(NSEvent *)event {
+  [self engine]->process_key_event(event.keyCode, true);
+}
+
+@end

+ 36 - 2
util/gameutils.xcodeproj/project.pbxproj

@@ -10,8 +10,8 @@
 		CD62FD04229195FF00376440 /* files.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD62FD02229195FF00376440 /* files.cxx */; settings = {COMPILER_FLAGS = "-fvisibility=default"; }; };
 		CD62FD082291988F00376440 /* osx_env.mm in Sources */ = {isa = PBXBuildFile; fileRef = CD62FD072291988F00376440 /* osx_env.mm */; settings = {COMPILER_FLAGS = "-fvisibility=default"; }; };
 		CD79A0F927246F4500CAFFC2 /* state_machine.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD79A0F727246F4500CAFFC2 /* state_machine.cxx */; };
-		CD79A0FA27246F4500CAFFC2 /* state_machine.h in Headers */ = {isa = PBXBuildFile; fileRef = CD79A0F827246F4500CAFFC2 /* state_machine.h */; };
 		CD7E884822960F4800D877FE /* AppKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CD7E884722960F4800D877FE /* AppKit.framework */; };
+		CD9F1BB92B744A370035EDF5 /* game in Headers */ = {isa = PBXBuildFile; fileRef = CD62FCFF2291953700376440 /* game */; settings = {ATTRIBUTES = (Public, ); }; };
 /* End PBXBuildFile section */
 
 /* Begin PBXContainerItemProxy section */
@@ -55,6 +55,11 @@
 		CD79A0F727246F4500CAFFC2 /* state_machine.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = state_machine.cxx; sourceTree = "<group>"; };
 		CD79A0F827246F4500CAFFC2 /* state_machine.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = state_machine.h; sourceTree = "<group>"; };
 		CD7E884722960F4800D877FE /* AppKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AppKit.framework; path = System/Library/Frameworks/AppKit.framework; sourceTree = SDKROOT; };
+		CD9F1B8D2B743DC20035EDF5 /* hash.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = hash.hpp; sourceTree = "<group>"; };
+		CD9F1B8E2B743DC20035EDF5 /* time.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = time.hpp; sourceTree = "<group>"; };
+		CD9F1B8F2B743DC20035EDF5 /* env.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = env.hpp; sourceTree = "<group>"; };
+		CD9F1B902B743DC20035EDF5 /* files.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = files.hpp; sourceTree = "<group>"; };
+		CD9F1B912B743DC20035EDF5 /* identity.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = identity.hpp; sourceTree = "<group>"; };
 		CDA31AB32725C6190071CDC2 /* smart_map.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = smart_map.hpp; sourceTree = "<group>"; };
 /* End PBXFileReference section */
 
@@ -75,6 +80,7 @@
 			children = (
 				CD62FCE622904AD500376440 /* GoogleMock.xcodeproj */,
 				CD62FCFF2291953700376440 /* game */,
+				CD9F1B8A2B743DC20035EDF5 /* include */,
 				CD3AC70A1D2C0726002B4BB0 /* src */,
 				CD3AC7091D2C0726002B4BB0 /* Products */,
 				CD7E884622960F4700D877FE /* Frameworks */,
@@ -121,6 +127,34 @@
 			name = Frameworks;
 			sourceTree = "<group>";
 		};
+		CD9F1B8A2B743DC20035EDF5 /* include */ = {
+			isa = PBXGroup;
+			children = (
+				CD9F1B8B2B743DC20035EDF5 /* game */,
+			);
+			path = include;
+			sourceTree = "<group>";
+		};
+		CD9F1B8B2B743DC20035EDF5 /* game */ = {
+			isa = PBXGroup;
+			children = (
+				CD9F1B8C2B743DC20035EDF5 /* util */,
+			);
+			path = game;
+			sourceTree = "<group>";
+		};
+		CD9F1B8C2B743DC20035EDF5 /* util */ = {
+			isa = PBXGroup;
+			children = (
+				CD9F1B8D2B743DC20035EDF5 /* hash.hpp */,
+				CD9F1B8E2B743DC20035EDF5 /* time.hpp */,
+				CD9F1B8F2B743DC20035EDF5 /* env.hpp */,
+				CD9F1B902B743DC20035EDF5 /* files.hpp */,
+				CD9F1B912B743DC20035EDF5 /* identity.hpp */,
+			);
+			path = util;
+			sourceTree = "<group>";
+		};
 /* End PBXGroup section */
 
 /* Begin PBXHeadersBuildPhase section */
@@ -128,7 +162,7 @@
 			isa = PBXHeadersBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
-				CD79A0FA27246F4500CAFFC2 /* state_machine.h in Headers */,
+				CD9F1BB92B744A370035EDF5 /* game in Headers */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};