-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
026878f
commit 151de93
Showing
4 changed files
with
87 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
const Window = require("./lib/window.js"); | ||
const App = require("./lib/app.js"); | ||
|
||
module.exports = Window; | ||
module.exports = App; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,65 @@ | ||
const EventEmitter = require("events"); | ||
const sdl = require("./binding"); | ||
const InitFlag = require("./types").InitFlag; | ||
const eventWatcher = require("./events"); | ||
|
||
class ApplicationContext extends EventEmitter { | ||
static mainContext = null; | ||
static loopId = null; | ||
static mainLoop(delayMs = 17) { | ||
if (ApplicationContext.loopId) return; | ||
const Window = require("./window"); | ||
|
||
const Status = { | ||
Loaded: 0x10, | ||
Idle: 0x11, | ||
Started: 0x12, | ||
Stopped: 0x13, | ||
}; | ||
|
||
class App { | ||
static status = Status.Loaded; | ||
|
||
static mainLoop(eventWatcher, delayMs = 17) { | ||
if (App.status == Status.Started) { | ||
console.log("->Main Loop already active!!"); | ||
} else if (App.status != Status.Idle) { | ||
App.initSDL(); | ||
} | ||
App.status = Status.Started; | ||
(function loop() { | ||
if (App.status == Status.Stopped) return; | ||
eventWatcher.pollEvent(); | ||
ApplicationContext.loopId = setTimeout(loop, delayMs); | ||
setTimeout(loop, delayMs); | ||
})(); | ||
} | ||
|
||
constructor(windowList) { | ||
super(); | ||
this.windowList = windowList; | ||
static initSDL() { | ||
if (sdl.init(InitFlag.VIDEO) !== 0) { | ||
console.log("->Unable to initalise SDL"); | ||
this.exit(); | ||
App.status = Status.Stopped; | ||
App.exit(); | ||
return; | ||
} else { | ||
App.status = Status.Idle; | ||
console.log("->Success of initalised SDL"); | ||
} | ||
|
||
// handle interrupt signal ( Ctrl + C ) | ||
// destroy all window, then exits | ||
|
||
process.on("SIGINT", () => { | ||
for (let [id, win] in this.windowList) { | ||
for (let [, win] in Window.list) { | ||
win.close(); | ||
} | ||
}); | ||
} | ||
|
||
console.log("->Success of initalised SDL"); | ||
static exit() { | ||
if (App.status == Status.Stopped) { | ||
console.log("->Unable, already exited!!"); | ||
} else if (Window.list.size == 0) { | ||
App.status = Status.Stopped; | ||
sdl.quit(); | ||
console.log("->Success to exit SDL"); | ||
} | ||
} | ||
|
||
exit() { | ||
if (this.windowList.size > 0) return; | ||
clearTimeout(ApplicationContext.loopId); | ||
sdl.quit(); | ||
console.log("->Success to exit SDL"); | ||
process.exit(0); | ||
static createWindow(opts) { | ||
return Window.create(App, opts); | ||
} | ||
} | ||
|
||
module.exports = ApplicationContext; | ||
module.exports = App; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,48 @@ | ||
const windowList = require("../window").windowList; | ||
const EventType = require("../types").EventType; | ||
const WindowEvent = require("./window_event"); | ||
const MouseEvent = require("./mouse_event"); | ||
const KeyboardEvent = require("./keyboard_event"); | ||
// const DropEvent = require("./drop_event"); | ||
const EventWatcher = require("../binding").EventWatcher; | ||
|
||
const eventWatcher = new EventWatcher(); | ||
|
||
let lastEventWindowId; | ||
|
||
eventWatcher.setCallback(function(eventType, winId) { | ||
|
||
let win = windowList.get(winId); | ||
|
||
if (!win) { | ||
winId = lastEventWindowId; | ||
win = windowList.get(winId); | ||
return; | ||
} | ||
|
||
function eventHandler(eventWatcher, eventType, window) { | ||
switch (eventType) { | ||
case EventType.WINDOWEVENT: | ||
WindowEvent.initWindowEvents(eventWatcher, win); | ||
WindowEvent.initWindowEvents(eventWatcher, window); | ||
break; | ||
case EventType.KEYDOWN: | ||
case EventType.KEYUP: | ||
KeyboardEvent.initKeyboardEvents(eventWatcher, win); | ||
KeyboardEvent.initKeyboardEvents(eventWatcher, window); | ||
break; | ||
case EventType.MOUSEMOTION: | ||
case EventType.MOUSEBUTTONDOWN: | ||
case EventType.MOUSEBUTTONUP: | ||
case EventType.MOUSEWHEEL: | ||
MouseEvent.initMouseEvents(eventWatcher, win); | ||
break; | ||
// case EventType.DROPFILE: | ||
// case EventType.DROPTEXT: | ||
// case EventType.DROPBEGIN: | ||
// case EventType.DROPCOMPLETE: | ||
// DropEvent.initDropEvents(eventWatcher.getWindowEvent); | ||
MouseEvent.initMouseEvents(eventWatcher, window); | ||
break; | ||
// case EventType.DROPFILE: | ||
// case EventType.DROPTEXT: | ||
// case EventType.DROPBEGIN: | ||
// case EventType.DROPCOMPLETE: | ||
// DropEvent.initDropEvents(eventWatcher.getWindowEvent); | ||
// break; | ||
case EventType.QUIT: | ||
//win.close(); | ||
break; | ||
} | ||
lastEventWindowId = winId; | ||
} | ||
|
||
function setupEventWatcher(windowList) { | ||
const eventWatcher = new EventWatcher(); | ||
|
||
eventWatcher.setCallback((eventType, winId) => { | ||
let window = windowList.get(winId); | ||
|
||
if (!window) return; | ||
|
||
}); | ||
eventHandler(eventWatcher, eventType, window); | ||
}); | ||
return eventWatcher; | ||
} | ||
|
||
module.exports = eventWatcher; | ||
module.exports = { setupEventWatcher }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters