Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "remoting/client/plugin/touch_input_scaler.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "remoting/proto/event.pb.h" | |
| 9 | |
| 10 namespace remoting { | |
| 11 | |
| 12 using protocol::TouchEvent; | |
| 13 using protocol::TouchEventPoint; | |
| 14 | |
| 15 namespace { | |
| 16 | |
| 17 // |value| is the number to be scaled. |output_max| is the output desktop's max | |
| 18 // height or width. |input_max| is the input desktop's max height or width. | |
| 19 float Scale(float value, int output_max, int input_max) { | |
| 20 DCHECK_GT(output_max, 0); | |
| 21 DCHECK_GT(input_max, 0); | |
| 22 value *= output_max; | |
| 23 value /= input_max; | |
| 24 return value; | |
| 25 } | |
| 26 | |
| 27 // Same as Scale() but |value| will be scaled and clamped using |output_max| and | |
| 28 // |input_max|. | |
| 29 float ScaleAndClamp(float value, int output_max, int input_max) { | |
| 30 value = Scale(value, output_max, input_max); | |
| 31 return std::max(0.0f, std::min(static_cast<float>(output_max), value)); | |
| 32 } | |
| 33 | |
| 34 } // namespace | |
| 35 | |
| 36 TouchInputScaler::TouchInputScaler(InputStub* input_stub) | |
| 37 : InputFilter(input_stub) { | |
| 38 } | |
| 39 | |
| 40 TouchInputScaler::~TouchInputScaler() { | |
|
Wez
2015/02/05 02:09:06
nit: You can put the {} on the same line since the
Rintaro Kuroiwa
2015/02/06 23:35:00
oh wow I didn't know about git cl format :)
| |
| 41 } | |
| 42 | |
| 43 void TouchInputScaler::InjectTouchEvent(const TouchEvent& event) { | |
| 44 if (input_size_.is_empty() || output_size_.is_empty()) | |
| 45 return; | |
| 46 | |
| 47 // We scale based on the maximum input & output coordinates, rather than the | |
| 48 // input and output sizes, so that it's possible to reach the edge of the | |
| 49 // output when up-scaling. We also take care to round up or down correctly, | |
| 50 // which is important when down-scaling. | |
| 51 TouchEvent out_event(event); | |
| 52 for (int i = 0; i < out_event.touch_points().size(); ++i) { | |
| 53 TouchEventPoint* point = out_event.mutable_touch_points(i); | |
| 54 if (point->has_x() || point->has_y()) { | |
| 55 DCHECK(point->has_x() && point->has_y()); | |
| 56 point->set_x( | |
| 57 ScaleAndClamp(point->x(), output_size_.width(), input_size_.width())); | |
| 58 point->set_y(ScaleAndClamp(point->y(), | |
| 59 output_size_.height(), | |
| 60 input_size_.height())); | |
| 61 } | |
| 62 | |
| 63 if (point->has_radius_x() || point->has_radius_y()) { | |
|
Wez
2015/02/05 02:09:06
nit: Suggest adding a comment to explain why we ne
Rintaro Kuroiwa
2015/02/06 23:35:00
Done.
| |
| 64 DCHECK(point->has_radius_x() && point->has_radius_y()); | |
| 65 point->set_radius_x( | |
| 66 Scale(point->radius_x(), output_size_.width(), input_size_.width())); | |
| 67 point->set_radius_y(Scale(point->radius_y(), | |
| 68 output_size_.height(), | |
| 69 input_size_.height())); | |
| 70 } | |
| 71 } | |
| 72 | |
| 73 InputFilter::InjectTouchEvent(out_event); | |
| 74 } | |
| 75 | |
| 76 } // namespace remoting | |
| OLD | NEW |