| Development/Tutorials/Qt4 Ruby Tutorial/Chapter 12 | Current message text |
| ↓...Chapter 12/Page display title/fi | Development/Tutorials/Qt4 Ruby Tutorial/Chapter 12 |
| ↓...t4 Ruby Tutorial/Chapter 12/1/fi | TutorialBrowser |
| ↓...t4 Ruby Tutorial/Chapter 12/2/fi | Qt4 Ruby -oppikurssi |
| ↓...t4 Ruby Tutorial/Chapter 12/3/fi | Hanging in the Air the Way Bricks Don't |
| ↓...t4 Ruby Tutorial/Chapter 12/4/fi | Oppikurssi 11 - Ammutaan tykillä |
| ↓...t4 Ruby Tutorial/Chapter 12/5/fi | Oppikurssi 13 - Peli loppui |
| ↓...t4 Ruby Tutorial/Chapter 12/6/fi | == Hanging in the Air the Way Bricks Don't == |
| ↓...t4 Ruby Tutorial/Chapter 12/7/fi | [[Image:Qt4_Ruby_Tutorial_Screenshot_12.png|center]] |
| ↓...t4 Ruby Tutorial/Chapter 12/8/fi | Tiedostot: |
| ↓...t4 Ruby Tutorial/Chapter 12/9/fi | === Yleiskuva === |
| ↓...4 Ruby Tutorial/Chapter 12/10/fi | In this example, we extend our '''<tt>LCDRange</tt>''' class to include a text label. We also provide something to shoot at. |
| ↓...4 Ruby Tutorial/Chapter 12/11/fi | === Läpikäynti rivi riviltä === |
| ↓...4 Ruby Tutorial/Chapter 12/12/fi | This constructor first calls '''<tt>init()</tt>''' and then sets the label text. '''<tt>init()</tt>''' is a separate function performing initialization mosty because of function overloading matters in the original C++ version. |
| ↓...4 Ruby Tutorial/Chapter 12/13/fi | The setup of '''<tt>lcd</tt>''' and '''<tt>slider</tt>''' is the same as in the previous chapter. Next we create a [http://doc.qt.nokia.com/latest/qlabel.html Qt::Label] and tell it to align the contents centered horizontally and to the top vertically. The [http://doc.qt.nokia.com/latest/qobject.html#connect Qt::Object::connect()] calls have also been taken from the previous chapter. |
| ↓...4 Ruby Tutorial/Chapter 12/14/fi | This function sets the label text. |
| ↓...4 Ruby Tutorial/Chapter 12/15/fi | The '''<tt>CannonField</tt>''' now has two new signals: '''<tt>hit()</tt>''' and '''<tt>missed()</tt>'''. In addition, it contains a target. |
| ↓...4 Ruby Tutorial/Chapter 12/16/fi | The '''<tt>hit()</tt>''' signal is emitted when a shot hits the target. The '''<tt>missed()</tt>''' signal is emitted when the shot moves beyond the right or bottom edge of the widget (i.e., it is certain that it has not and will not hit the target). |
| ↓...4 Ruby Tutorial/Chapter 12/17/fi | This line has been added to the constructor. It creates a "random" position for the target. In fact, the '''<tt>newTarget()</tt>''' function will try to paint the target. Because we are in a constructor, the '''<tt>CannonField</tt>''' widget is invisible. Qt guarantees that no harm is done when calling [http://doc.qt.nokia.com/latest/qwidget.html#update Qt::Widget::update()] on a hidden widget. |
| ↓...4 Ruby Tutorial/Chapter 12/18/fi | This function creates a target center point at a new random position. |
| ↓...4 Ruby Tutorial/Chapter 12/19/fi | We create the [http://doc.qt.nokia.com/latest/qtime.html Qt::Time] object '''<tt>midnight</tt>''', which represents the time 00:00:00. Next we fetch the number of seconds from midnight until now and use it as a random seed. See the documentation for [http://doc.qt.nokia.com/latest/qdate.html Qt::Date], [http://doc.qt.nokia.com/latest/qtime.html Qt::Time], and [http://doc.qt.nokia.com/latest/qdatetime.html Qt::DateTime] for more information. |
| ↓...4 Ruby Tutorial/Chapter 12/20/fi | Finally we calculate the target's center point. We keep it within the rectangle (x = 200, y = 35, width = 190, height = 255), i.e., the possible x and y values are 200 to 389 and 35 to 289, respectively) in a coordinate system where we put y position 0 at the bottom edge of the widget and let y values increase upwards x is as normal, with 0 at the left edge and with x values increasing to the right. |
| ↓...4 Ruby Tutorial/Chapter 12/21/fi | By experimentation we have found this to always be in reach of the shot. |
| ↓...4 Ruby Tutorial/Chapter 12/22/fi | This part of the timer event has not changed from the previous chapter. |
| ↓...4 Ruby Tutorial/Chapter 12/23/fi | This '''<tt>if</tt>''' statement checks whether the shot rectangle intersects the target rectangle. If it does, the shot has hit the target (ouch!). We stop the shoot timer and emit the '''<tt>hit()</tt>''' signal to tell the outside world that a target was destroyed, and return. Note that we could have created a new target on the spot, but because the '''<tt>CannonField</tt>''' is a component we leave such decisions to the user of the component. |
| ↓...4 Ruby Tutorial/Chapter 12/24/fi | This is the same as in the previous chapter, except that it now emits the '''<tt>missed()</tt>''' signal to tell the outside world about the failure. |
| ↓...4 Ruby Tutorial/Chapter 12/25/fi | And the rest of the function is as before. |
| ↓...4 Ruby Tutorial/Chapter 12/26/fi | '''<tt>CannonField::paintEvent()</tt>''' is as before, except that this has been added: |
| ↓...4 Ruby Tutorial/Chapter 12/27/fi | This line makes sure that the target is also painted when necessary. |
| ↓...4 Ruby Tutorial/Chapter 12/28/fi | This function paints the target; a rectangle filled with red and with a black outline. |
| ↓...4 Ruby Tutorial/Chapter 12/29/fi | This private function returns the enclosing rectangle of the target. Remember from '''<tt>newTarget()</tt>''' that the '''<tt>target</tt>''' point uses y coordinate 0 at the bottom of the widget. We calculate the point in widget coordinates before we call [http://doc.qt.nokia.com/latest/qrect.html#moveCenter Qt::Rect::moveCenter()]. |
| ↓...4 Ruby Tutorial/Chapter 12/30/fi | The reason we have chosen this coordinate mapping is to fix the distance between the target and the bottom of the widget. Remember that the widget can be resized by the user or the program at any time. |
| ↓...4 Ruby Tutorial/Chapter 12/31/fi | There are no new members in the '''<tt>MyWidget</tt>''' class, but we have slightly changed the constructor to set the new '''<tt>LCDRange</tt>''' text labels. |
| ↓...4 Ruby Tutorial/Chapter 12/32/fi | We set the angle text label to "ANGLE". |
| ↓...4 Ruby Tutorial/Chapter 12/33/fi | We set the force text label to "FORCE". |
| ↓...4 Ruby Tutorial/Chapter 12/34/fi | === Sovelluksen suorittaminen === |
| ↓...4 Ruby Tutorial/Chapter 12/35/fi | The '''<tt>LCDRange</tt>''' widgets look a bit strange: When resizing '''<tt>MyWidget</tt>''', the built-in layout management in [http://doc.qt.nokia.com/latest/qvboxlayout.html Qt::VBoxLayout] gives the labels too much space and the rest not enough; making the space between the two '''<tt>LCDRange</tt>''' widgets change size. We'll fix that in the next chapter |
| ↓...4 Ruby Tutorial/Chapter 12/36/fi | === Harjoitukset === |
| ↓...4 Ruby Tutorial/Chapter 12/37/fi | Make a cheat button that, when pressed, makes the '''<tt>CannonField</tt>''' display the shot trajectory for five seconds. |
| ↓...4 Ruby Tutorial/Chapter 12/38/fi | If you did the "round shot" exercise from the previous chapter, try changing the '''<tt>shotRect()</tt>''' to a '''<tt>shotRegion()</tt>''' that returns a [http://doc.qt.nokia.com/latest/qregion.html Qt::Region] so you can have really accurate collision detection. |
| ↓...4 Ruby Tutorial/Chapter 12/39/fi | Make a moving target. |
| ↓...4 Ruby Tutorial/Chapter 12/40/fi | Make sure that the target is always created entirely on-screen. |
| ↓...4 Ruby Tutorial/Chapter 12/41/fi | Make sure that the widget cannot be resized so that the target isn't visible. [Hint: [http://doc.qt.nokia.com/latest/qwidget.html#minimumSize-prop Qt::Widget::setMinimumSize()] is your friend.] |
| ↓...4 Ruby Tutorial/Chapter 12/42/fi | Not easy; make it possible to have several shots in the air at the same time. [Hint: Make a '''<tt>Shot</tt>''' class.] |
| ↓...4 Ruby Tutorial/Chapter 12/43/fi | [[Category:Ruby]] |