| 1 | #pragma once
|
|---|
| 2 |
|
|---|
| 3 | #include "../ModuleCommon.h"
|
|---|
| 4 |
|
|---|
| 5 | namespace mil {
|
|---|
| 6 |
|
|---|
| 7 | /**
|
|---|
| 8 | * public members should be thread safe
|
|---|
| 9 | */
|
|---|
| 10 | template <typename Child>
|
|---|
| 11 | class UIModule : public ModuleCommon<UIModule<Child>, thread::Cocoa> {
|
|---|
| 12 | public:
|
|---|
| 13 | Tls tls;
|
|---|
| 14 | MIL_CRTP_CLASS_MEMBERS;
|
|---|
| 15 |
|
|---|
| 16 | public:
|
|---|
| 17 | template <typename T>
|
|---|
| 18 | class Event : boost::noncopyable {
|
|---|
| 19 | public:
|
|---|
| 20 | thread::id_t getThreadId() {
|
|---|
| 21 | return pool::MemoryHeader::get((void*)this).getThreadId();
|
|---|
| 22 | }
|
|---|
| 23 | typedef T DataType;
|
|---|
| 24 | T data;
|
|---|
| 25 | void (*execute)(void* event, void* target);
|
|---|
| 26 | };
|
|---|
| 27 |
|
|---|
| 28 | private:
|
|---|
| 29 | bool repost;
|
|---|
| 30 |
|
|---|
| 31 | protected:
|
|---|
| 32 | NSWindow *window;
|
|---|
| 33 |
|
|---|
| 34 | public:
|
|---|
| 35 | NSWindow* getWindow() {
|
|---|
| 36 | return window;
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | void setWindow(NSWindow* window_) {
|
|---|
| 40 | window = window_;
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | void requestExit(void* event_memory) {
|
|---|
| 44 | exitNotifier.tls.flush();
|
|---|
| 45 | ExitEvent e;
|
|---|
| 46 | post(e, exitNotifier, event_memory);
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | UIModule() : tls(this->thread_id), repost(false), window(NULL) {
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | void createGui() {
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | void run() {
|
|---|
| 56 | MIL_MODULE_GET_SUPER_CHILD(*this).loop();
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | protected:
|
|---|
| 60 | void repostEvent() {
|
|---|
| 61 | repost = true;
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | template <typename Data, typename Sender>
|
|---|
| 65 | static void dispatcher(void* event_, void* target_) {
|
|---|
| 66 | typedef Child Target;
|
|---|
| 67 | Target& target = *static_cast<Target*>(target_);
|
|---|
| 68 | Event<Data>& event = *static_cast<Event<Data>*>(event_);
|
|---|
| 69 | target.repost = false;
|
|---|
| 70 | ModuleExecuteProxy::execute<Data, Target>(event.data, target);
|
|---|
| 71 | if (target.repost) {
|
|---|
| 72 | target.post(event.data, target);
|
|---|
| 73 | return;
|
|---|
| 74 | }
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | public:
|
|---|
| 78 | void requestExit() {
|
|---|
| 79 | ExitEvent e;
|
|---|
| 80 | post(e, *this);
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | template <typename Sender>
|
|---|
| 84 | NSWindow* getCachedWindow(Sender& sender, bool flush = false) {
|
|---|
| 85 | NSWindow* window = NULL;
|
|---|
| 86 |
|
|---|
| 87 | if (flush || !(window = sender.tls.getData(this->thread_id).window_cache)) {
|
|---|
| 88 | window = getWindow();
|
|---|
| 89 | if (!window) {
|
|---|
| 90 | return NULL;
|
|---|
| 91 | }
|
|---|
| 92 | sender.tls.getData(this->thread_id).window_cache = window;
|
|---|
| 93 | }
|
|---|
| 94 | return window;
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | template <typename T, typename Sender>
|
|---|
| 98 | void post(const T& event, Sender& sender, void* memory = NULL) {
|
|---|
| 99 | NSWindow* window = getCachedWindow(sender);
|
|---|
| 100 | Event<T> e;
|
|---|
| 101 | e.data = event;
|
|---|
| 102 | e.execute = dispatcher<T, Sender>;
|
|---|
| 103 | }
|
|---|
| 104 | public:
|
|---|
| 105 | void destroy() {
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | ~UIModule() {
|
|---|
| 109 | do_auto_join();
|
|---|
| 110 | if (!window) {
|
|---|
| 111 | return;
|
|---|
| 112 | }
|
|---|
| 113 | [window dealloc];
|
|---|
| 114 | window = NULL;
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | bool execute_front() {
|
|---|
| 118 | return false;
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | void mouseDown(NSEvent*) {
|
|---|
| 122 | }
|
|---|
| 123 | protected:
|
|---|
| 124 | void loop() {
|
|---|
| 125 | [[NSRunLoop currentRunLoop] run];
|
|---|
| 126 | }
|
|---|
| 127 | };
|
|---|
| 128 |
|
|---|
| 129 | }
|
|---|