Skip to content

Commit b260364

Browse files
committed
Add timer display component
1 parent 021bbdf commit b260364

4 files changed

Lines changed: 93 additions & 0 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
timer-display {
2+
position: absolute;
3+
top: 0;
4+
width: 100%;
5+
height: 0.5rem;
6+
7+
& div {
8+
background: rgba(255, 255, 255, 0.2);
9+
}
10+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { Component } from './../../lib/Component';
2+
import { Util } from '@aegis-framework/artemis';
3+
4+
class TimerDisplay extends Component {
5+
6+
constructor (...args) {
7+
super (...args);
8+
9+
this.props = {
10+
callback: () => {},
11+
time: 0,
12+
step: 0,
13+
timer: null,
14+
tick: null
15+
};
16+
17+
this.state = {
18+
elapsed: 0,
19+
remaining: 0,
20+
value: 100
21+
};
22+
}
23+
24+
willMount () {
25+
this.setProps ({
26+
step: this.props.time / 100
27+
});
28+
29+
this.setState ({
30+
remaining: this.props.time
31+
});
32+
33+
return Promise.resolve ();
34+
}
35+
36+
didMount () {
37+
this.setProps ({
38+
tick: () => {
39+
this.setProps({
40+
timer: setTimeout (() => {
41+
if (this.state.elapsed >= this.props.time) {
42+
Util.callAsync (this.props.callback, this.engine).then (() => {
43+
clearTimeout (this.props.timer);
44+
if (this.parentNode) {
45+
this.element ().remove ();
46+
}
47+
});
48+
} else {
49+
this.setState ({
50+
elapsed: this.state.elapsed + this.props.step,
51+
remaining: this.state.remaining - this.props.step,
52+
value: (1 - (this.state.elapsed / this.props.time)) * 100
53+
});
54+
55+
this.forceRender ();
56+
this.props.tick ();
57+
}
58+
59+
60+
}, this.props.step)
61+
});
62+
}
63+
});
64+
65+
this.props.tick ();
66+
67+
return Promise.resolve ();
68+
}
69+
70+
render () {
71+
return `
72+
<div style="width: ${this.state.value}%;"></div>
73+
`;
74+
}
75+
}
76+
77+
TimerDisplay.tag = 'timer-display';
78+
79+
80+
export default TimerDisplay;

src/index.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,5 @@
4444
@import './components/slot-container/index.css';
4545
@import './components/text-box/index.css';
4646
@import './components/text-input/index.css';
47+
@import './components/timer-display/index.css';
4748
@import './components/visual-novel/index.css';

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ import SettingsScreen from './components/settings-screen';
122122
import SlotContainer from './components/slot-container';
123123
import TextBox from './components/text-box';
124124
import TextInput from './components/text-input';
125+
import TimerDisplay from './components/timer-display';
125126
import VisualNovel from './components/visual-novel';
126127

127128
Monogatari._components = [
@@ -145,6 +146,7 @@ Monogatari._components = [
145146
SlotContainer,
146147
TextBox,
147148
TextInput,
149+
TimerDisplay,
148150
VisualNovel,
149151
];
150152

0 commit comments

Comments
 (0)