Skip to content

Commit

Permalink
Added [N] to example to create new window
Browse files Browse the repository at this point in the history
  • Loading branch information
tonsky committed May 6, 2021
1 parent 50f3791 commit e7258f2
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 138 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ Build:
Run examples:

```
./script/run.py SingleWindow
./script/run.py Example
```
90 changes: 58 additions & 32 deletions examples/SingleWindow.java → examples/Example.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
import org.jetbrains.jwm.macos.*;
import org.jetbrains.skija.*;

public class SingleWindow {
public class Example implements Consumer<Event> {
public Window window;
public ContextMetal contextMtl;
public DirectContext directContext;

public int angle = 0;
public Font font = new Font(FontMgr.getDefault().matchFamilyStyleCharacter(null, FontStyle.NORMAL, null, "↑".codePointAt(0)), 12);
public Font font48 = new Font(FontMgr.getDefault().matchFamilyStyle(null, FontStyle.BOLD), 48);
Expand All @@ -28,7 +29,15 @@ public class SingleWindow {
public double[] times = new double[180];
public int timesIdx = 0;

public Example() {
window = App.makeWindow();
window.setEventListener(this);
changeContext();
window.show();
}

public void paint() {
// System.out.println(System.currentTimeMillis() + " " + "paint()");
if (contextMtl == null || directContext == null)
return;

Expand Down Expand Up @@ -87,16 +96,33 @@ public void paint() {
// HUD
try (var paint = new Paint()) {
canvas.save();
canvas.translate(width - (8 + 180 + 8 + 8), height - (8 + 24 + 24 + 32 + 8 + 8));
canvas.translate(width - (8 + 180 + 8 + 8), height - (8 + 24 + 24 + 24 + 24 + 32 + 8 + 8));

// bg
paint.setColor(0x80000000);
canvas.drawRRect(RRect.makeXYWH(0, 0, 8 + 180 + 8, 8 + 24 + 24 + 32 + 8, 4), paint);
paint.setColor(0x40000000);
canvas.drawRRect(RRect.makeXYWH(0, 0, 8 + 180 + 8, 8 + 24 + 24 + 24 + 24 + 32 + 8, 4), paint);
canvas.translate(8, 8);

// Variant
paint.setColor(0x80000000);
canvas.drawRRect(RRect.makeXYWH(0, 0, 16, 16, 2), paint);
paint.setColor(0xFFFFFFFF);
canvas.drawString("↓↑", 0, 12, font, paint);
canvas.drawString(variants[variantIdx], 24, 12, font, paint);
canvas.translate(0, 24);

paint.setColor(0x80000000);
canvas.drawRRect(RRect.makeXYWH(0, 0, 16, 16, 2), paint);
paint.setColor(0xFFFFFFFF);
canvas.drawString("↓↑ " + variants[variantIdx], 0, 12, font, paint);
canvas.drawString("N", 3, 12, font, paint);
canvas.drawString("New window", 24, 12, font, paint);
canvas.translate(0, 24);

paint.setColor(0x80000000);
canvas.drawRRect(RRect.makeXYWH(0, 0, 16, 16, 2), paint);
paint.setColor(0xFFFFFFFF);
canvas.drawString("W", 3, 12, font, paint);
canvas.drawString("Close window", 24, 12, font, paint);
canvas.translate(0, 24);

int frames = 0;
Expand Down Expand Up @@ -152,38 +178,38 @@ public void changeContext() {
directContext = DirectContext.makeMetal(contextMtl.getDevicePtr(), contextMtl.getQueuePtr());
}

public void run() {
App.init();
window = App.makeWindow();
changeContext();
window.setEventListener(e -> {
if (e instanceof EventClose) {
window.close();
@Override
public void accept(Event e) {
if (e instanceof EventClose) {
window.close();
if (App._windows.size() == 0)
App.terminate();
} else if (e instanceof EventKeyboard) {
EventKeyboard eventKeyboard = (EventKeyboard) e;
if (eventKeyboard.isPressed() == true) {
if (eventKeyboard.getKeyCode() == 53) { // Esc
window.close();
} else if (e instanceof EventKeyboard) {
EventKeyboard eventKeyboard = (EventKeyboard) e;
if (eventKeyboard.isPressed() == true) {
if (eventKeyboard.getKeyCode() == 45) { // n
new Example();
} else if (eventKeyboard.getKeyCode() == 13 || eventKeyboard.getKeyCode() == 53) { // w || Esc
window.close();
if (App._windows.size() == 0)
App.terminate();
} else if (eventKeyboard.getKeyCode() == 125) { // ↓
variantIdx = (variantIdx + 1) % variants.length;
changeContext();
} else if (eventKeyboard.getKeyCode() == 126) { // ↑
variantIdx = (variantIdx + variants.length - 1) % variants.length;
changeContext();
} else
System.out.println("Key pressed: " + eventKeyboard.getKeyCode());
}
} else if (e instanceof EventPaint) {
paint();
} else if (eventKeyboard.getKeyCode() == 125) { // ↓
variantIdx = (variantIdx + 1) % variants.length;
changeContext();
} else if (eventKeyboard.getKeyCode() == 126) { // ↑
variantIdx = (variantIdx + variants.length - 1) % variants.length;
changeContext();
} else
System.out.println("Key pressed: " + eventKeyboard.getKeyCode());
}
});
window.show();
App.runEventLoop();
} else if (e instanceof EventPaint) {
paint();
}
}

public static void main(String[] args) {
new SingleWindow().run();
App.init();
new Example();
App.runEventLoop();
}
}
102 changes: 0 additions & 102 deletions examples/MultipleWindows.java

This file was deleted.

2 changes: 1 addition & 1 deletion macos/cc/WindowMac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
// Create Cocoa window
constexpr int initialWidth = 800;
constexpr int initialHeight = 600;
NSRect windowRect = NSMakeRect(100, 100, initialWidth, initialHeight);
NSRect windowRect = NSMakeRect(100 + rand() % 100, 100 + rand() % 100, initialWidth, initialHeight);

NSUInteger windowStyle = NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskResizable | NSWindowStyleMaskMiniaturizable;

Expand Down
4 changes: 2 additions & 2 deletions script/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def main():
os.chdir(os.path.dirname(__file__) + '/..')

parser = argparse.ArgumentParser()
parser.add_argument('example', nargs='?', default='SingleWindow')
parser.add_argument('example', nargs='?', default='Example')
args = parser.parse_args()

space = 'https://packages.jetbrains.team/maven/p/skija/maven'
Expand All @@ -20,7 +20,7 @@ def main():
# '/Users/prokopov/ws/skija/native/build',
# '/Users/prokopov/ws/skija/shared/target/classes',
]
sources = ['examples/' + args.example + '.java']
sources = glob.glob('examples/*.java')
common.javac(compile_classpath, sources, 'examples/target/classes')

run_classpath = [
Expand Down

0 comments on commit e7258f2

Please sign in to comment.