contrib/model_viewer: use `show_splash_screen`
[nit.git] / contrib / model_viewer / src / model_viewer.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Portable game to destroy asteroids
16 module model_viewer is
17 app_name "Model Viewer"
18 app_namespace "org.nitlanguage.model_viewer"
19 app_version(1, 0, git_revision)
20
21 android_manifest_activity """android:screenOrientation="landscape""""
22 android_api_target 15
23 end
24
25 import gamnit::depth
26
27 import globe
28
29 redef class App
30
31 # All available models
32 var models: Array[Model] = [
33 new LeafModel(new Cube, new SmoothMaterial.default),
34 new LeafModel(new Mesh.uv_sphere(4.0, 32, 16), new SmoothMaterial.default),
35 new LeafModel(new Mesh.uv_sphere(4.0, 32, 16), new NormalsMaterial),
36 new Model("models/Tree_01.obj"),
37 new Model("models/Oak_Fall_01.obj"),
38 new Model("models/Quandtum_BA-2_v1_1.obj"),
39 new GlobeModel]
40
41 # Index of the current model in `models`
42 var model_index = 0
43
44 # Texture "Previous model"
45 var ui_prev = new Texture("ui/prev.png")
46
47 # Texture "Next model"
48 var ui_next = new Texture("ui/next.png")
49
50 redef fun on_create
51 do
52 super
53
54 # Show splash screen
55 var logo = new Texture("splash.png")
56 show_splash_screen logo
57
58 if args.length > 0 then
59 # Load a model passed as the first command line argument
60 var model_path = args.first
61 if model_path.has_prefix("assets/") then model_path = model_path.substring_from(7)
62
63 var model = new Model(model_path)
64 models.unshift model
65 end
66
67 world_camera.near = 1.0
68 world_camera.far = 1000.0
69
70 for model in models do model.load
71 for texture in asset_textures_by_name.values do texture.load
72
73 # Display the first model
74 model = models[model_index]
75
76 # Setup UI
77 # Use 800 px in height as screen reference
78 ui_camera.reset_height 800.0
79
80 var prev_sprite = new Sprite(ui_prev,
81 ui_camera.bottom_left.offset(200, -40, 0))
82 prev_sprite.scale = 0.5
83 ui_sprites.add prev_sprite
84
85 var next_sprite = new Sprite(ui_next,
86 ui_camera.bottom_right.offset(-165, -40, 0))
87 next_sprite.scale = 0.5
88 ui_sprites.add next_sprite
89 end
90
91 # Set the currently displayed model
92 fun model=(model: Model)
93 do
94 var actor = new Actor(model, new Point3d[Float](0.0, 0.0, 0.0))
95
96 model = model.leaves.first
97 actor.center.x -= model.mesh.center.x
98 actor.center.y += model.mesh.center.y
99 actor.center.z -= model.mesh.center.z
100
101 var height = model.mesh.dimensions.y
102 world_camera.reset_height(height * 3.0)
103
104 actors.clear
105 actors.add actor
106 end
107
108 # Cycle to the next or previous model, changing the index by `d`
109 fun cycle_model(d: Int)
110 do
111 model_index = (model_index + d + models.length) % models.length
112 model = models[model_index]
113 end
114
115 redef fun accept_event(event)
116 do
117 var display = display
118 if display == null then return super
119
120 if event isa QuitEvent then
121 exit 0
122 else if event isa KeyEvent and event.is_down then
123 if event.is_arrow_right then
124 cycle_model 1
125 else if event.is_arrow_left then
126 cycle_model -1
127 end
128 else if event isa PointerEvent and event.pressed then
129 if event.x.to_i > display.width / 2 then
130 cycle_model 1
131 else cycle_model -1
132 end
133
134 return super
135 end
136
137 private var clock = new Clock
138
139 redef fun update(dt)
140 do
141 super
142
143 var t = clock.total.to_f
144
145 # Rotate the model
146 actors.first.rotation = t
147
148 # Move the light source
149 var dist_to_light = 20.0
150 t *= 1.33
151 light.position.x = dist_to_light * t.cos
152 light.position.y = 4.0
153 light.position.z = dist_to_light * t.sin
154 end
155 end