Why Delphi? Mobile Apps Are Everywhere
Total Page:16
File Type:pdf, Size:1020Kb
Expert Delphi Robust and fast cross-platform application development Paweł Głowacki BIRMINGHAM - MUMBAI Expert Delphi Copyright © 2017 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: June 2017 Production reference: 1300617 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78646-016-5 www.packtpub.com Credits Author Copy Editors Paweł Głowacki Safis Editing Muktikant Garimella Reviewer Project Coordinator Dave Nottage Vaidehi Sawant Commissioning Editor Proofreader Kunal Parikh Safis Editing Acquisition Editor Indexer Nitin Dasan Aishwarya Gangawane Content Development Editor Graphics Anurag Ghogre Abhinash Sahu Technical Editors Production Coordinator Madhunikita Sunil Chindarkar Shantanu Zagade Rutuja Vaze Foreword I have known and worked with Paweł Głowacki for more than 16 years. Paweł is one of the world-wide Delphi community leading experts. In this book, Expert Delphi, Paweł takes you on a learning journey that started in the early 1980s with Compas Pascal, Poly Pascal, and Turbo Pascal. For more than 20 years, Delphi has continued to evolve to meet the needs of developers around the world. During most of this time, Paweł has been a key team member and advocate for the continuous innovation of the compilers, frameworks, runtime library, integrated development environment, and tool chain, allowing developers to easily build applications across desktop, server, mobile, internet, and cloud platforms. With FireMonkey (FMX), developers can build on top of a modern multiplatform framework based on a rich history of objects, components, and libraries and that has included Turbo Vision, Object Windows, and the VCL. Leveraging these frameworks, libraries, and tools allows developers to create applications for a wide range of architectures, databases, and distributed computing environments. In his presentations and examples, Paweł shows you how easy it is to create a wide range of applications using SQL/NoSQL databases, XML, and JSON. Paweł also helps developers create multitier applications using the latest technologies, including REST. As a member of and a leading advocate for the large, global Delphi community and ecosystem, Paweł is uniquely qualified to show you how to become a mobile developer superhero, become a hyper-productive software engineer, and build one source code application that can be compiled to all major mobile platforms and form factors. Paweł also shows you how to build stunning 2D and 3D multidevice graphical user interfaces with the FireMonkey library, providing an outstanding user experience for your application's users. With Expert Delphi, you'll learn the best practices for writing high-quality, reliable, and maintainable code with Delphi's Object Pascal language and component architecture. After you complete Paweł's book, you'll understand how to take full advantage of mobile operating systems, frameworks, and hardware capabilities, including working with sensors and the Internet of Things. Beyond developing apps for desktops and smartphones, you'll learn how to quickly and easily integrate with cloud services and data using REST APIs and JSON. You'll also learn how to architect and deploy powerful mobile backend services. In Expert Delphi, Paweł encapsulates the knowledge gained through years as a world-class Delphi engineer, an entertaining presenter, a community leader, and a passionate advocate. With his words, step-by-step instructions, screenshots, source code snippets, examples, and links to additional sources of information, you will learn how to continuously enhance your skills and apps. Become a developer superhero and build stunning cross-platform apps with Delphi. David Intersimone "David I" Vice President of Developer Communities, Evans Data Corp. and Embarcadero Community MVP. Santa Cruz, California, USA About the Author Paweł Głowacki is Embarcadero's European Technical Lead for Developer Tools. Previously, he spent over 7 years working as a senior consultant and trainer for Delphi within Borland Education Services and CodeGear. In addition to working with Embarcadero customers across the region, he represents Embarcadero internationally as a conference and seminar speaker. About the Reviewer Dave Nottage is an independent software consultant who lives in Adelaide, South Australia. Developing software with Delphi since 1995 when Delphi 1 was first released, he has been a valued member of Embarcadero's TeamB since 2001, and an Embarcadero MVP since 2014. Dave is considered an expert in Delphi and has shared his expertise as a speaker at Delphi developer conferences, including BorCon, Delphi Live, and ADUG Symposia. He is an active member on Embarcadero forums and Stack Overflow, and his popular blog articles, which can be found at Delphi Worlds, provide solutions for common Delphi problems. His blog is followed by many developers across the globe and is considered a go-to source. Having worked on projects for major global enterprises, corporations, and governments, he is now working on his own commercial product, a set of code libraries that aids developers in cross-platform development. In his spare time, Dave enjoys playing bass guitar in a band, art exhibitions, puzzles that challenge the mind, and traveling the world. Dave can be contacted at [email protected]. www.PacktPub.com For support files and downloads related to your book, please visit www.PacktPub.com. Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks. h t t p s ://w w w . p a c k t p u b . c o m /m a p t Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career. Why subscribe? Fully searchable across every book published by Packt Copy and paste, print, and bookmark content On demand and accessible via a web browser Customer Feedback Thanks for purchasing this Packt book. At Packt, quality is at the heart of our editorial process. To help us improve, please leave us an honest review on this book's Amazon page at h t t p s ://w w w . a m a z o n . c o m /d p /1786460165. If you'd like to join our team of regular reviewers, you can e-mail us at [email protected]. We award our regular reviewers with free eBooks and videos in exchange for their valuable feedback. Help us be relentless in improving our products! For my beloved wife, Barbara, and son, Mateusz. Thank you for all your love and support. I love you! Table of Contents Preface 1 Chapter 1: Fasten Your Seat Belts 8 Delphi installation 8 Delphi versions 9 Running the Delphi installer 10 Installing custom components 14 IDE options 16 Riding the Integrated Development Environment 17 Delphi compilers and toolchains 17 Hello World app 19 Deploying to mobile devices 32 Deploying to Android 32 Deploying to iOS 36 Summary 42 Chapter 2: Mind Your Language 43 Do you speak Object Pascal? 43 Program structure 44 Console application 45 Forms applications 49 Object Pascal Phrase Book 62 Tokens 62 Constants 63 Types 64 Sets and arrays 65 Helpers 66 Generics 68 Anonymous code 72 Operator overloading 74 Custom attributes 75 Runtime Type Information 76 Summary 77 Chapter 3: Packing Up Your Toolbox 78 Parallel Programming Library 78 Parallel loops 79 Tasks 80 Futures 82 Working with files 84 JSON 90 Writing JSON 91 Reading JSON 95 XML 98 Summary 105 Chapter 4: Playing with FireMonkey 106 Drawing in code 107 Get moving with timers 114 Precise timing 116 The power of parenting 118 Shapes 118 Animations 121 Programmable effects 123 Touch me 124 Touch 125 Gestures 127 Multitouch 129 Game of Memory 131 Designing the game 131 Working with images 132 Designing a user interface 134 Aligning, anchoring, and margins 135 Layouts 136 Building the games main form 136 Storing the game's configuration 144 The game's settings form 147 Summary 149 Chapter 5: FireMonkey in 3D 150 Cross-platform 3D rendering 150 Using Context3D 152 Custom Wireframe component 157 Objects 3D 161 Moving Earth 163 Building an interactive 3D scene 167 Using 3D models 174 [ ii ] Starfield simulation 177 Mixing 3D and 2D 180 Summary 182 Chapter 6: Building User Interfaces with Style 183 Working with