5 ms·
Your project is quite impressive. And welcome to the world of computer graphics! I'd consider taking inspiration from the following sources: 1. GUI toolkits l
by minhmeoke 5y ago
Your project is quite impressive. And welcome to the world of computer graphics!
I'd consider taking inspiration from the following sources:
1. GUI toolkits like QT QML [1] or Android [2]. These typically build a hierarchical tree of different components (eg: start with a root window, which contains panes, which in turn contain text and buttons). Each component may contain different properties (eg: font, color), and properties may be inherited from the parent component.
Advantages:
+ preserves semantics of component properties and how they are linked to each other (eg: the caption is below the image)
Disadvantages:
- complexity: building a layout/constraint engine can be difficult, or alternatively you can use absolute positioning with relative offsets which can be tedious to use (in this case the layer-based approach below might make more sense).
[1] https://en.wikipedia.org/wiki/QML https://en.wikipedia.org/wiki/QML
[2] https://developer.android.com/guide/topics/ui/declaring-layout https://developer.android.com/guide/topics/ui/declaring-layo...
2. Graphical editor programs like Gimp or Photoshop, or Adobe Flash.
These build up a screen as a collection of vertically stacked layers or assets (eg: graphics, text, etc) with attached properties and optionally bounding boxes. Higher layers/assets occlude the content of the layers below them, so you would need to implement some kind of visibility logic.
Advantages:
+ simplicity
+ you can use identifiers for assets, and therefore don't need to perform pixel-by-pixel comparisons.
Disadvantages:
- may lose some information about how different components are related to each other
Also, rather than a raster pixel-based representation, it might make sense to use a vector representation internally [3]. The most popular vector representation is SVG. The full spec is very verbose, so you probably only want to implement a small subset of it. This would permit you to specify properties like line thickness, color, striped/dotted patterns. At render time, you could convert the (proportional) fonts to vectors as well for consistency, and then rasterize the entire scene when rendering to a display surface. But for testing, it would be better to use the scene graph / vector format which is easier for users to reason about.
[3] https://en.wikipedia.org/wiki/Vector_graphics https://en.wikipedia.org/wiki/Vector_graphics
[4] http://blog.leahhanson.us/post/recursecenter2016/haiku_icons.html http://blog.leahhanson.us/post/recursecenter2016/haiku_icons...
But perhaps this is over-complicating things.
- akkartik 5y agoThank you for those suggestions! Do you know if any of those tools following either approach has any automated tests? My immediate problem is how to manage the complexity of implementing a layout engine or editor. Somewhere I need something checking that a given asset identifier results in specific pixels. And I'd like the tests for _that_ to be nice to read. It's a bit of a chicken-and-egg problem..
- minhmeoke 5y agoGlad that you found this to be useful! Android includes the Espresso UI testing framework [1]. Essentially, you can specify matchers that compare your expected values or predicates against an actual object identified by an R.id identifier. It's very powerful (since you can write your own custom matchers) but can be cumbersome to use [2]. [1] https://developer.android.com/training/testing/espresso/basics https://developer.android.com/training/testing/espresso/basi... [2] Example Espresso Test: https://github.com/android/testing-samples/blob/main/ui/espresso/BasicSample/app/src/test/java/com/example/android/testing/espresso/BasicSample/ChangeTextBehaviorLocalTest.java#L47 https://github.com/android/testing-samples/blob/main/ui/espr... https://github.com/android/testing-samples https://github.com/android/testing-samples Alternatively, Squish [3] is a very polished and more elegant commercial testing tool that lets you record test-cases using a GUI tool and convert them into (ideally modularized) methods that verify object properties or compare (masked) screenshots of the GUI: [3] https://www.froglogic.com/squish/features/ https://www.froglogic.com/squish/features/ Demo video (starting at 14:24): https://youtu.be/ElH-3MVHPRw?t=864 https://youtu.be/ElH-3MVHPRw?t=864 They abstract away a lot of the functionality using the Gherkin [4] domain-specific language so that tests are easier to read at a high level (but you can still dig down into the underlying programmatic implementation). [4] https://cucumber.io/docs/guides/overview/ https://cucumber.io/docs/guides/overview/ This is probably too much complexity for your use-case, but may provide some ideas or inspiration for what is possible. Perhaps a simplified matcher-style system might be a good starting point though.